2016-11-25 79 views
1

我试着制作一个简单的骰子滚动程序,我可以让骰子'滚动',但我无法阻止它,它只会保持,如果你输入任何东西。我试图添加它,所以如果你说'不'或'n'返回并像往常一样去我的其他功能。我说回来是因为继续我的其他代码很重要。谢谢。循环不退出,如果roll_more =='no'

import random 
min = 1 
def rolling6(): 
    roll_more = "yes" 
    while roll_more == "yes" or roll_more =="y": 
     max = 6 
     print("Rolling the dices...") 
     print("The values are...") 
     print(random.randint(min, max)) 
     print(random.randint(min, max)) 
     roll = input("Roll the die again? ") 

    if roll_more == "no" or roll_more == "n": 
     return 
    #roll_again = "yes" 
    #while roll_again == "yes" or roll_again =="y": 
    # print("Rolling the dices...") 
    # print("The values are...") 
    # print(random.randint(min, 6)) 
    # print(random.randint(min, 6)) 
    # roll_again = input("Roll the die again? ") 

    #if roll_again == "no" or roll_again == "n": 
    # return 


x = input("Use six sided die? ") 
while x == "yes" or x =="y": 
    rolling6() 
+1

用户输入分配给变量'roll'并且您正在检查'roll_more'。请注意,检查也可以像''如果roll_more.lower()in ['yes','y']完成:'以节省空间并确保使用小写字母。您的代码会将“否”的值理解为“是”。 –

回答

1

您写这封信是roll代替roll_more,所以你总是对常量“是”检查。

此外,您的xroll_more变量不包含您所期望的值。首先,你输入“是”进入循环,因为rolling6永远不会被调用。然后,你输入rolling6循环。当你通过输入“no”退出时,你退出到外部循环,阅读x,它的值仍然是“yes”(因为你从未在任何地方覆盖它),这意味着你不会跳出该循环,并且您再次输入rolling6

您可能想要将roll更改为roll_more并将while x == "yes" or x =="y":更改为if x == "yes" or x =="y":

此外,您的if roll_more == "no" or roll_more == "n": return是多余的,因为该语句后没有代码。