2013-11-20 39 views
0

嗨,我正在寫一個骰子滾動程序,並想知道如何將其循環回到最後的程序開始。任何解決方案都將大大降低。由於循環程序回到開始?

import random 
while True: 
    dice_type = int(input("would you like to roll a 4,6 or 12 sided dice:")) 
    if dice_type in [4, 6, 12]: 
     break 
    print("Sorry that is not the correct input") 

score = random.randint(1, dice_type) 

print ("You have chosen to throw a %d-sided dice" %dice_type) 
print ("You rolled a %d" %score) 
+0

你有無限多嘗試在另一個while循環中包裝你的語句? –

+0

我該怎麼做? –

+0

將所有內容都包裹在True循環中。只需在'import random'下寫'While True:'並在其下面縮進一切。 – Tobberoth

回答

0
import random 
while True: 
    while True: 
     dice_type = int(input("would you like to roll a 4,6 or 12 sided dice:")) 
     if dice_type in [4, 6, 12]: 
      break 
     print("Sorry that is not the correct input") 

    score = random.randint(1, dice_type) 

    print ("You have chosen to throw a %d-sided dice" %dice_type) 
    print ("You rolled a %d" %score) 

將讓玩家選擇的芯片類型,並顯示成績的時間(禁止宇宙中,按Ctrl-C,或設備或電源故障的熱死)

+0

它的作品感謝大家的快速反應。 –

+0

如果它適合你,請考慮將答案標記爲正確。 –

+0

好的就可以了。再次感謝。 –