2014-01-07 15 views
0

我正在嘗試創建一個接受輸入並將轉換任何輸入的程序,例如,是/是以小寫形式在下面的while循環中被接受。我試圖做到這一點,但沒有奏效。任何想法?將輸入轉換爲小寫以便在while循環中接受

#Import the random function - This only needs to be imported once. 
import random 

#Use a while loop to allow the user to repeat the process 
repeat = "YES" 

while repeat == "yes": 

#User is able to input which sided dice they want to throw.  
    dice = input("What side dice do you want to use? 4, 6, or 12?\n") 
#4 sided 
    if dice == "4": 
     #Outputs what sided dice has been chosen and the score thay they rolled. 
     print(dice, "sided dice chose.\nYou rolled a", random.randint(1,4)) 
#6 sided 
    elif dice == "6": 
     print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,6))) 
#12 sided 
    elif dice == "12": 
     print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,12))) 
#Incorrect value entered 
    else: 
     #Informs the user that the number they have chosen is not a valid option. 
     print(dice, "is not a valid choice") 
#Asks user if they want to use the program again. 
    print("Do you want to use the program again? Yes or No?") 
    #Links back to the start of the while loop. 
    repeat = input() 
+1

'輸入()運行。低()' –

+0

這似乎是一個問題你的重複初始值是大寫;) – GreenAsJade

+0

你嘗試了什麼?您在發佈的代碼中不會顯示您的嘗試。 – user2357112

回答

1

您的代碼甚至不會進入while循環,當你重複設置爲「YES」,然後立即檢查它是否是「是」,這是不。

1

我會以另一種方式

while True: 
    dice = input(....) 
    #etc 
    repeat = '' 
    while repeat.lower() not in ['yes', 'no']: 
     repeat = input('Do you want to use the program again? (yes/no)?') 
    if repeat.lower() == 'no': 
     break 

這會要求用戶輸入yes或no,並繼續提問,直到是已經解決了這個或沒有在提供。

0

丹尼爾是完全正確的。該'YES'在代碼的開頭應被格式化爲'yes'因爲顯然'YES不等於'yes',因此while循環不會在所有