Y.chong - 1 month ago 6x

Python Question

`import tkinter`

import random

def rollDice():

x=random.randint(1,6)

total=0

if x == 1:

total+=1

....

print(total)

I would like to add each number I roll in i .rollDice and store into "total" ,and maximum is 50 ,how can i do that , thanks

Answer

As @PaulRooney says, i am not sure what `tkinter`

has to do with this. From what i understand, you want to roll a dice until your `total`

reaches or exceeds 50.. So here is my take on this:

```
from random import randint
def rollDice():
return randint(1,6)
total = 0
while total < 50:
new_roll = rollDice()
total += new_roll
print('You rolled a {}. The new total is {}'.format(new_roll, total))
# You rolled a 3. The new total is 3
# You rolled a 3. The new total is 6
# You rolled a 4. The new total is 10
# You rolled a 6. The new total is 16
# You rolled a 2. The new total is 18
# You rolled a 1. The new total is 19
# You rolled a 5. The new total is 24
# You rolled a 5. The new total is 29
# You rolled a 4. The new total is 33
# You rolled a 5. The new total is 38
# You rolled a 2. The new total is 40
# You rolled a 3. The new total is 43
# You rolled a 4. The new total is 47
# You rolled a 6. The new total is 53
```

Source (Stackoverflow)

Comments