jeudi 11 octobre 2018

Rolling 2 dice in Python and if they are the same number, roll again and continueing

Hi I'm a Python noobie and got a question about simulating rolling 2 dice:

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, everytime 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.




Aucun commentaire:

Enregistrer un commentaire