So I need to write a Python program where I need to roll 2 dice and print the sum of the 2 dice. I got this so far:
import random
def monopoly():
x = random.randrange(1,7)
y = random.randrange(1,7)
while True:
if x != y:
print(x, '+', y, '=', x+y)
break
Now, every time the 2 dice numbers are the same (2 + 2 or 3 + 3 etc.) You can throw again. If 3 times in a row the dice are the same, you need to go to jail. I thought I had to work with a while loop using continue like this:
else:
if x == y:
print(x + y)
continue
#continuation of the code above
Now if I do have an outcome where the dice are the same it keeps printing out the sum over and over again until I stop the program myself. But I don't know why.
How do I fix This?, because I have no idea how to do this.
You need new random numbers in each loop iteration:
while True:
x = random.randrange(1,7)
y = random.randrange(1,7)
if x != y:
print(x, '+', y, '=', x+y)
break
Otherwise, x
and y
will never change and so your breaking condition will never hold.