python中while循环的多个条件
问题描述:
我在 python 中遇到了包含多个带有 while
循环的语句的问题.它在单个条件下工作得很好,但是当我包含多个条件时,循环不会终止.我在这里做错了吗?
I am having problems including multiple statements with while
loop in python. It works perfectly fine with single condition but when i include multiple conditions, the loop does not terminate. Am i doing something wrong here?
name = raw_input("Please enter a word in the sentence (enter . ! or ? to end.)")
final = list()
while (name != ".") or (name != "!") or (name != "?"):
final.append(name)
print "...currently:", " ".join(final)
name = raw_input("Please enter a word in the sentence (enter . ! or ? to end.)")
print " ".join(final)
答
需要使用和
;如果满足所有条件,而不仅仅是一个条件,您希望循环继续:
You need to use and
; you want the loop to continue if all conditions are met, not just one:
while (name != ".") and (name != "!") and (name != "?"):
但是您不需要括号.
最好在这里测试会员资格:
Better would be to test for membership here:
while name not in '.!?':