2014-06-26 56 views
-2

我正在嘗試朗姆酒模擬5個骰子的程序。我知道我錯過了一些簡單的東西,但我不能把它放在手指上。我試圖改變骰子整數,但它只打印輸出中的數字沒有被添加。我做了什麼???代碼在這裏,輸出如下。任何幫助都會很棒。提前致謝。在python中添加數字的問題

# This program will simulate dice rolls 5 different 
# times and add the total of each roll. 

import random 
MIN = 1 
MAX = 6 
ROLLS = 5 
def main(): 
    print(random.randint(MIN, MAX)) 
    print(random.randint(MIN, MAX)) 

    for count in range(ROLLS): 
     if random.randint(MIN,MAX)== 2: 
      print('The total is two!') 
     elif random.randint(MIN,MAX)== 3: 
      print('The total is three!') 
     elif random.randint(MIN,MAX) == 4: 
      print('The total is four!') 
     elif random.randint(MIN,MAX) == 5: 
      print('The total is five!') 
     elif random.randint(MIN,MAX) == 6: 
      print('The total is six!') 
     elif random.randint(MIN,MAX) == 7: 
      print('The total is seven!') 
     elif random.randint(MIN,MAX) == 8: 
      print('The total is eight!') 
     elif random.randint(MIN,MAX) == 9: 
      print('The total is nine!') 
     elif random.randint(MIN,MAX) == 10: 
      print('The total is ten!') 
     elif random.randint(MIN,MAX) == 11: 
      print('The total is eleven!') 
     else: 
      print('The total is twelve!') 





     print('The dice will be rolled and the total shown') 
     print('each time enter is pressed up to five times!') 

     total = point(MIN, MAX) 
     print('The total is', total) 




def point(MIN, MAX): 
    for count in range(MIN, MAX): 

     dicerolls = 0.0 
     total = 0.0 
     dice = input('') 
     total += dicerolls 

     return total 



main() 
2 
2 
The total is twelve! 
The dice will be rolled and the total shown 
each time enter is pressed up to five times! 

The total is 0.0 
The total is three! 
The dice will be rolled and the total shown 
each time enter is pressed up to five times! 

The total is 0.0 
The total is twelve! 
The dice will be rolled and the total shown 
each time enter is pressed up to five times! 

The total is 0.0 
The total is six! 
The dice will be rolled and the total shown 
each time enter is pressed up to five times! 

The total is 0.0 
The total is twelve! 
The dice will be rolled and the total shown 
each time enter is pressed up to five times! 

The total is 0.0 
+3

每次你進行比較,你再次調用'randint',它會返回一個不同的隨機值。將結果放入變量中。 – Ryan

+0

你的骰子滾動滾動,變成一個永不停止改變現值的球。 –

回答

1

每當您撥打random.randint(MIN,MAX)時,您都會得到一個新的隨機數。

你需要做的是這樣的:

for count in range(ROLLS): 
    dice = random.randint(MIN, MAX) 

    if dice == 2: 
      print('The total is two!') 

    if dice == 3: 
    .... 
0

根據您的程序邏輯,每個「骰子」你在沒有產生價值的卷使其能夠循環。因此,你不能保證得到五卷。

您能否看到每撥打random.randint(MIN,MAX)正在爲每個呼叫
生成一個新的隨機數?換句話說,在每次執行random.randint(MIN,MAX)時,在你的循環中你可以得到不同的數字,因此在沒有打印語句的情況下退出循環。

例如,假設這是你的循環隨機生成的數字列表: 3,2,2,2,2,2,2,2,2,2,2,2

環路會在沒有比賽的情況下完成比賽,因爲您每次擲出的對手都會產生一次隨機數字,最多爲12次。因此,從本質上講,由於缺少更好的術語,你會得到一個空輥

編輯: 也根據您的最小/最大值,你只產生從1-6,而不是1-12的隨機值。

'# This program will simulate dice rolls 5 different 
# times and add the total of each roll. 

import random 
MIN = 2 #You cannot roll a 1 
MAX = 12 
ROLLS = 5 
def main(): 

    totalRolled = 0 # Accumulator 

    for count in range(ROLLS) 
     rolledValue = random.randint(MIN,MAX) 
     print(rolledValue + " was rolled") 

     if(rolledValue == 2): 
      totalRolled =+ rolledValue 
     if(rolledValue == 3): 
      totalRolled += rolledValue 

     #...Etc 

     print("the total is" + totalRolled)