Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何返回到Python循环的顶部_Loops_Python 3.x_If Statement - Fatal编程技术网

Loops 如何返回到Python循环的顶部

Loops 如何返回到Python循环的顶部,loops,python-3.x,if-statement,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(in

我需要一些关于在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))

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

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

callme()
callme()

或者查看for或while循环。

如果您想反复运行程序的该部分(有效地跳回到顶部),您可以使用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))

嗯,代码中没有循环,所以不能“返回到循环的顶部”。