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
#11
(Aug-07-2019, 06:22 PM)ichabod801 Wrote: The error you are getting is what the assignment talks about with validating user code. You need to validate the input before using it. I don't know what you have covered. Normally I would use try/except to validate, but you may not have gotten to that:
 
while True: value = input('enter number: ') try: value = int(value) break except ValueError: print('numbers only please') 
If you haven't covered that, the isdigit method of strings will return True if the string only contains digits 0-9. And yes, you need a loop in main to keep asking the questions. Also, you need to return the hit or miss status from target_num to main, and then use it to decide whether or not to roll for damage.

Yeah, learning about validation was light. Instead of doing any actual work with it we had a quiz on other things besides that. So it was a lot of - read this pseudocode and good luck.

I've incorporated the code you posted. And I initially posted a problem I ran into, but I managed to work out the problem to get things working right.

import random

print("")

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


def main():
    target_num()

main()
So the first module/function is working finally. I think it's safe to move on to the second part then. Not to just figure out how to properly call back to the successful hit so that the damage dice module/function appears only with that.

Damn it, I spoke too soon. 2 dice aren't rolling and I can't fix it.
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, 08:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with Loops in lab assignment jonstryder 6 3,411 Jul-28-2019, 06:40 PM
Last Post: ThomasL
  Array to get profit (school assignment) harryvandervelden 2 2,855 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