0
因此,我目前正在學習如何編寫Python代碼。我決定做一個小程序,它要求用戶輸入一個詞來命令程序做某些事情。儘管把這個詞的每個變體都放入代碼中,但我一直都有這個問題。我的問題是,我怎樣才能防止這種情況?任何幫助,將不勝感激。請記住,我仍在學習Python,可能無法理解所有內容。這裏是我的代碼:如何使Python 3.4輸入不敏感?
#This is the main program.
if choice == '1':
print ("Not Availble Yet")
print (" ")
time.sleep(2.5)
main()
#This is if you wish to quit.
if choice =='2':
end = input ("Are you sure you'd like to quit? ")
#These are all the "I'd like to quit" options.
if end == 'yes':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == 'Yes':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == 'yEs':
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == ("yeS"):
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
if end == ("YES"):
print ("Closing Program in 5 seconds").lower
time.sleep(5)
quit
#These are all the "I wouldn't like to quit" options.
if end == 'no':
print ("Continuing Program").lower
time.sleep(2.5)
main()
謝謝!
的if/elif的開關之前小寫用戶輸入。哦,並且確實將它切換到if/elif struct,這會使它更具可讀性。 – 2014-10-18 19:49:01
請注意,「print(...)。lower」不起作用的原因至少有兩個。 – jonrsharpe 2014-10-18 19:50:30
要銳化@jonrsharpe留下的評論 - 您需要用圓括號實際調用lower()函數。簡單地把'lower'這個單詞返回給函數本身,而不是實際使字符串小寫。我不太清楚還有什麼其他原因使它無法工作......也許他會願意詳細說明嗎? – Lix 2014-10-18 19:52:30