2016-09-19 67 views
-2
import tkinter 
import random 

def rollDice(): 
    x=random.randint(1,6) 
    total=0 
    if x == 1: 
    total+=1 
    .... 
print(total) 

我想補充每個號碼我在.rollDice卷和存儲到total總數,以及最大爲50。我怎樣才能做到這一點?如何我搖骰子和存儲使用Python3.4

+0

你是什麼意思_maximum是50_?在「總數」變爲50或更多或擲骰子50次之前?你也不需要檢查'x'的值。只需將它添加到總數中,如下所示:'total + = x' –

+1

這聽起來像您要求我們做功課。 –

+1

不知道這與tkinter有什麼關係。你已經有了'+ ='運算符,所以進入while循環(總數小於或等於50 I.e' <='),並且每次添加骰子滾動的結果。 –

回答

0

正如@PaulRooney所說,我不知道tkinter與此有什麼關係。據我瞭解,你要滾骰子直到你total達到或超過50 ..所以這裏是我對這個:

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 
+0

Thanks亞:D 如果可能添加一個按鈕「滾動骰子」? – yc1

+0

這是可能的,但這最好是作爲一個單獨的問題提出,因爲它與本文無關。 – holdenweb

+0

@holdenweb好的,謝謝你。 – yc1