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
#12
You've got your query loop inside your die roll loop. You need to roll the dice and then ask the question (or vice versa). As it is you are asking the question each time you roll a single die. Take the whole while True loop and unindent it one level.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
(Aug-08-2019, 01:43 AM)ichabod801 Wrote: You've got your query loop inside your die roll loop. You need to roll the dice and then ask the question (or vice versa). As it is you are asking the question each time you roll a single die. Take the whole while True loop and unindent it one level.

Thanks. Ok, its working. I think that I was moving too much around, or maybe not enough around and just constantly frustrating myself with it. My wife made me take a break since I was about ready to start pulling my already thinning hair out.

Now for the second part, an array to roll damage dice. I'm guessing that it will be roughly built off the same process.
Reply
#14
(Aug-08-2019, 06:34 AM)jonstryder Wrote: I'm guessing that it will be roughly built off the same process.

Mostly. Just keep in mind that you're asking how many dice to roll. So the order needs to be ask, roll, determine results. Above you have roll, ask, determine results. Which is fine for what you have above, but the order needs to be changed for the damage part.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
Ok, it's a new day, and I got this working finally. At least I don't see any issues with it.

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("")
        return target_num()

def roll_damage():
    damage_dice = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    while True:
        roll = input("How many damage dice will you roll (1 to 12)?  ")
        try:
            roll = int(roll)
            break
        except ValueError:
            print("")
            print("Please enter a number.")
            print("")
    for y in range(roll):
        damage_dice[y] = random.randint(1, 6)
    print("The dice results:  ", damage_dice)
    print("Total damage is:  ", sum(damage_dice))
    print("")
    return


def main():
    while True:
        target_num()
        roll_damage()

main()
Reply


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