python中while循环的多个条件 [英] multiple conditions with while loop in python
本文介绍了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 '.!?':
这篇关于python中while循环的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文