Python Forum
Lab Assignment with Array's & Loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lab Assignment with Array's & Loops
#9
(Aug-06-2019, 10:57 PM)ichabod801 Wrote: I might just loop through what do you need to hit and how many damage dice. Like a helper for a DM running multiple monster attacks.

Ok, I've written something up for the first part of this.

__author__ = 'me'

import random

print("")

def target_num():
    while True:
        score = int(input("Enter the Target Number for your 3d6 Attack Roll.  "))
        dice = [0, 0, 0]
        for x in range(3):
            dice[x] = random.randint(1, 6)
        if sum(dice) <= score:
            print("Dice results:  ", dice)
            print("Dice results totaled:  ", sum(dice))
            print("Your attack roll HITS!")
            return score
        elif sum(dice) >= score:
            print("Dice results:  ", dice)
            print("Dice results totaled:  ", sum(dice))
            print("The attack roll MISSES!")
            return score
        else:
            print("You must enter a number.")

def main():
        target_num()

main()
If I input a number, everything works fine. But when I tested inputting something besides a number I get an error.

Traceback (most recent call last):
  File "G:/College Files/Current Classes/CIS - 122 - Intro to Programing Logic/projects/Lab5.py", line 29, in <module>
    main()
  File "G:/College Files/Current Classes/CIS - 122 - Intro to Programing Logic/projects/Lab5.py", line 27, in main
    target_num()
  File "G:/College Files/Current Classes/CIS - 122 - Intro to Programing Logic/projects/Lab5.py", line 8, in target_num
    score = int(input("Enter the Target Number for your 3d6 Attack Roll.  "))
ValueError: invalid literal for int() with base 10: 'm'
Now I'm guessing that it won't loop back to the start since the code is incomplete as I need to add a second and maybe third part (the damage die roll) to the program. Am I right in this assessment?

But to move on to the next half, if the attack hits then it needs to then display the damage dice to be rolled, but if it misses then I need it to start the attack roll over again.

I'm going to try and work on this next part, with a mind toward a person inputting a number properly instead of accidentally entering a letter.
Reply


Messages In This Thread
Lab Assignment with Array's & Loops - by jonstryder - Aug-06-2019, 06:17 PM
RE: Lab Assignment with Array's & Loops - by jonstryder - Aug-07-2019, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with Loops in lab assignment jonstryder 6 3,487 Jul-28-2019, 06:40 PM
Last Post: ThomasL
  Array to get profit (school assignment) harryvandervelden 2 2,892 Nov-28-2017, 05:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020