servqual != "great" or "good" or "lacking" or "poor"
相當於
(servqual != "great") or "good" or "lacking" or "poor"
而且因爲字符串被認爲True
,你總是得到它真正的
正確的方法是
servqual != "great" and servqual != "good" and servqual != "lacking" and servqual != "poor"
或
servqual not in ("great", "good", "lacking", "poor")
你的最終代碼看起來像
servqual = raw_input(">").lower()
while servqual not in ("great", "good", "lacking", "poor"):
print "I didn't understand that. Please try again."
servqual = raw_input(">").lower()
您可以進一步通過下面的步驟來改善它。
while raw_input(">").lower() not in ("great", "good", "lacking", "poor"):
print "I didn't understand that. Please try again."
謝謝,這工作得很好! – sfowler12 2014-09-20 22:00:30