[英]Python: Loop not exiting with “break”
我有一個while循環正在測試,以確保用戶輸入1、2或3。一旦用戶輸入1、2或3,循環就會無限循環,我無法擺脫它。
while True:
try:
filterselection = raw_input("Please select a filter (1, 2, or 3): ")
if filterselection == "1" or filterselection == "2" or filterselection == "3":
filterselection = int(filterselection)
break
else:
print "Not a valid number try again!"
except TypeError:
print "Lol, that's not a number try again!"
不要混用制表符和空格! 這是將原始代碼粘貼到顯示空白字符的編輯器中時看到的內容:
箭頭是制表符,而點是空格,請勿混用這些符號非常重要,因為如果您執行此操作,則看到的代碼可能不是Python解釋器看到的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.