我如何返回循环Python的顶部

时间:2015-07-13 20:40:45

标签: loops python-3.x if-statement

我需要一些帮助才能在python中返回循环顶部我知道break语句但是我不知道它是否有用。

这是我的代码:

import random

Which_Dice= input("What dice do you want? 4,6 or 12")

if input(Which_Dice)!=("4","6","12"):
    print("Please input the number 4,6 or 12")

elif int(Which_Dice)== 4:
    print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,5))

elif int(Which_Dice)== 6:
    print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,7))

elif int(Which_Dice)==12:
    print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,13))

2 个答案:

答案 0 :(得分:1)

您的代码中没有循环,但这可能会有所帮助吗?

def callme():
    Which_Dice= input("What dice do you want? 4,6 or 12")
    .
    .

callme()
callme()

或查看for或while循环。

答案 1 :(得分:1)

如果您想要做的是反复运行程序的那一部分(有效地跳回到顶部),您可以使用while true循环来实现:

import random

while True:

    Which_Dice= input("What dice do you want? 4,6 or 12")

    if input(Which_Dice)!=("4","6","12"):
        print("Please input the number 4,6 or 12")

    elif int(Which_Dice)== 4:
        print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,5))

    elif int(Which_Dice)== 6:
        print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,7))

    elif int(Which_Dice)==12:
        print(int(Which_Dice), "sided dice thrown", "score",random.randint(0,13))