我可以使用/ import更好吗?
2131427328
答案 0 :(得分:6)
您可以使用not in
。
while strLevel not in ["low", "medium", "high"]:
答案 1 :(得分:0)
确实,not in
是推荐的
但是你在问题中所做的比较是什么意思?
>>> StrLevel = 'high'
>>> StrLevel != "low" or "medium" or "high"
True
>>> StrLevel = 'medium'
>>> StrLevel != "low" or "medium" or "high"
True
>>> StrLevel = 'low'
>>> StrLevel != "low" or "medium" or "high"
'medium'
......可能根本不是你想要的。
简化一下:
>>> 'foo' != 'bar' or 'medium'
True
>>> 'foo' != 'foo' or 'medium'
'medium'
>>> False or 'medium'
'medium'
如果你不熟悉Python之前的语言中的布尔代数表达式,那会有点混乱。特别是因为Python在链接时遇到了使算术比较有意义的麻烦:
>>> x = 12
>>> 10 < x < 14
True
>>> 10 < x < 11
False