将程序循环回到开头?

时间:2013-11-20 13:03:46

标签: python-3.x

您好我正在编写一个骰子滚动程序,并想知道如何将其循环回到程序开头的最后。任何解决方案都会受到很大的影响。感谢

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)

1 个答案:

答案 0 :(得分: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或设备或电源故障)