My python program prints the first thing I wrote, and then loads forever and never prints the second task. What do I need to change with my code?
import random
def main():
money = 100
win = 0
loss = 0
draw = 0
bet = random.randint(5, 20)
print('You are starting with $100 and each round you will bet', bet, 'dollors')
while True:
x = random.randint(1, 6)
y = random.randint(1, 6)
z = x y
if money == 0 or money == 200:
break
if z == 7 or z == 11:
money = bet
win = 1
elif z == 2 or z == 3 or z == 12:
loss = 1
money -= bet
else:
draw = 1
print('You ended up with', money, 'dollars, and you won', win, 'rounds, lost', \
loss, 'rounds, and drew', draw, 'rounds')
main()
CodePudding user response:
Looks like it will never be able to satisfy all the conditions and is caught in a infinite loop
CodePudding user response:
I think you have to use correct condition in the if condition and i suggest u to use a var to run the loop and to break the loop.
import random
def main():
money = 100
win = 0
loss = 0
draw = 0
bet = random.randint(5, 20)
print('You are starting with $100 and each round you will bet', bet, 'dollors')
loop = True
while loop:
x = random.randint(1, 6)
y = random.randint(1, 6)
z = x y
if money <= 0 or money >= 200:
loop = False
if z == 7 or z == 11:
money = bet
win = 1
elif z == 2 or z == 3 or z == 12:
loss = 1
money -= bet
else:
draw = 1
print('You ended up with', money, 'dollars, and you won', win, 'rounds, lost', \
loss, 'rounds, and drew', draw, 'rounds')