![]() |
while loop question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: while loop question (/thread-29925.html) |
while loop question - KEYS - Sep-26-2020 Hello, When my program runs and reaches the winning condition of else: it will print both from the if and else as well as subtract the 1 as if it was a losing value. Need help with the order of operations. an example of the outcome
#dice game name = input("what is your name?") print ("Hello!",name) import random die1 = random.randint(1, 6) die2 = random.randint(1, 6) total = die1 + die2 money = 100 while total <10: if total <10: die1 = random.randint(1, 6) die2 = random.randint(1, 6) total = die1 + die2 money -=1 print ("you rolled a",total) print ("\tloser") print ("your cash is",money) else: money +=100 print("you rolled a",total) print("\twinner!") print("your cash is",money) RE: while loop question - bowlofred - Sep-26-2020 First of all, I don't understand why you have both 11 and 12. It seems like you don't need both. But your big problem is that you're changing the total after testing it, but before doing something with it. Imagine you got a 5 and entered the if portion. Then you change total to something else (like 11). it then removes 1 and prints "loser"without caring that total is now greater. RE: while loop question - KEYS - Sep-26-2020 Thanks for the reply, With your notes i was able to re order the code for a working result xd Thanks again. #dice game name = input("what is your name?") print ("Hello!",name) total = 0 money = 100 import random while money >=0: input("Press Enter to roll") die1 = random.randint(1, 6) die2 = random.randint(1, 6) total = die1 + die2 money -=1 if total <10: money -=20 print ("you rolled a",total) print ("\tloser") print ("your cash is",money) elif total >=10: money +=100 print("you rolled a",total) print("\twinner!") print("your cash is",money) |