0
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. However, even if the user types in 1 of these 5 options my code will still enter the while loop. 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

0

3 Answers 3

7
(x != y) or (x != z) ...

will always be true. Since you're in Python, I'd recommend using in instead:

if x in ['a', 'b', 'c']:
Sign up to request clarification or add additional context in comments.

3 Comments

Note that in the newer versions of Python (namely, 3.3 and up), you should use a set of values: if x in {'a', 'b', 'c'}:. Set literals are recognized as constants by the interpreter, so they are more efficient. Of course, this doesn't mean anything to the OP since he is using Python 2.x.
But what if they enter in a wrong answer again? I am unsure how to reset the for loop
while x not in... You might also start by setting x to None, so that test will fail first time and you don't need two input statements.
0

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)

Comments

0

Your code continues to enter into the loop as you're expecting 'weatherType' var to get one of your predefined values('WINDDIRECTION', etc). 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).

Alternatively, you could create a list with all your options ['WINDDIRECTION', '' etc ], and check if user input is in your list.

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: ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.