2016-11-25 77 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是多餘的,因爲該語句後沒有代碼。