我有這個while循環正在測試以確保用戶輸入1,2或3.一旦用戶輸入1,2或3,循環就會一直持續下去,我無法擺脫它。Python:循環不會隨着「break」退出
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!"
問題是...? –
您的縮進似乎不一致。 – sth
而不是多個'或',你可以使用'in'運算符並且說'如果filters(1','2','3'):'。爲了您的健康。 –