[英]While loop with multiple conditions in python
weatherType = raw_input('Enter a weather type: ')
while (weatherType != "WINDDIRECTION") or (weatherType != "WINDSPEED") or (weatherType != "AIRTEMPERATURE") or (weatherType != "WAVEHEIGHT") or (weatherType != "AIRPRESSURE"):
print "Sorry, invalid input. Please enter AIRTEMPERATURE, AIRPRESSURE, WAVEHEIGHT, WINDSPEED, or WINDDIRECTION for a city and either WINDDIRECTION, WINDSPEED, or AIRTEMPERATURE for an off shore bouy"
weatherType = raw_input('Enter a weather type: ')
Okay so with this loop, I am trying to get the user to input either WINDDIRECTION
, WINDSPEED
, AIRTEMPERATURE
, WAVEHEIGHT
, or AIRPRESSURE
. 好的,通过这个循环,我试图让用户输入WINDDIRECTION
, WINDSPEED
, AIRTEMPERATURE
, WAVEHEIGHT
或AIRPRESSURE
。 However, even if the user types in 1 of these 5 options my code will still enter the while loop. 但是,即使用户输入了这5个选项中的1个,我的代码仍然会进入while循环。 I don't know what is going on. 我不知道怎么回事。 I know I can use a for loop (for x in ["WINDDIRECTION", ....]) however a for loop will only work to see if their input is right the first time and if they type in a wrong answer again the code will continue 我知道我可以使用for循环(在[“ WINDDIRECTION”,....]中使用for循环),但是for循环仅可用于查看其输入是否正确,以及是否再次输入错误的答案。代码将继续
(x != y) or (x != z) ...
will always be true. 永远是真的。 Since you're in Python, I'd recommend using in
instead: 由于您使用的是Python,因此建议您改用in
:
if x in ['a', 'b', 'c']:
In addition to Lee Daniel Crocker. 除了李·丹尼尔·克罗克。
check out fabric package. 检查织物包装。 There are nice tools for managing console input. 有一些不错的工具可用于管理控制台输入。
from fabric.contrib import console
def validate(v):
answers = ['WINDDIRECTION',...]
if v in answers:
return int(v)
question = 'Enter a weather type: '
console_prompt = console.prompt(question, default="WINDDIRECTION", validate=validate)
Your code continues to enter into the loop as you're expecting 'weatherType' var to get one of your predefined values('WINDDIRECTION', etc). 您的代码继续进入循环,因为您期望'weatherType'变量会获得您的预定义值之一('WINDDIRECTION'等)。 However,it doesn't matter the input which user provides, the WHILE condition will always be satisfied (because 'weatherType' will have only one value at a time and it will match the while 'or' condition, hence entering the loop). 但是,用户提供的输入无关紧要,WHILE条件将始终得到满足(因为“ weatherType”一次仅具有一个值,并且将与while“ or”条件匹配,从而进入循环)。
Alternatively, you could create a list with all your options ['WINDDIRECTION', '' etc ], and check if user input is in your list. 或者,您可以创建一个包含所有选项的列表['WINDDIRECTION',''等],并检查用户输入是否在列表中。
Example: 例:
options = ["WINDDIRECTION", "WINDSPEED", "AIRTEMPERATURE", "WAVEHEIGHT", "WAVEHEIGHT"]
message = '''Sorry, invalid input. Please enter:
AIRTEMPERATURE, AIRPRESSURE,
WAVEHEIGHT, WINDSPEED, or WINDDIRECTION for a city and either
WINDDIRECTION, WINDSPEED, or AIRTEMPERATURE for an off shore bouy
'''
weatherType = raw_input('Enter a weather type: ')
while weatherType not in options:
print message
weatherType = raw_input('Enter a weather type: ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.