1

I'm not sure why this isn't working, but I have a feeling it has something to do with how I've structured the while loop. I want the loop to continue only if the user inputs something other than the two choices they have. However, even when I test it with putting in either of the two correct choices, the while loop continues.

prompt = "> "

print "Welcome to the converter. What would you like \
to convert? (temp or distance)"
choice = raw_input(prompt)

while (choice != "temp" or choice != "distance"):
    print "Sorry, that's not an option"
    choice = raw_input(prompt)
if choice == "temp":
    print "temp"
elif choice == "distance":
    print "distance"

What am I missing here? Thanks in advance.

4
  • 1
    If you want the if statement to be part of the while loop, you need to put it at the same indentation level. Commented Nov 14, 2015 at 17:43
  • 1
    It looks like you're just starting to learn python, you should really learn python 3, it's out for 10 years already, has fewer quirks and more features. Commented Nov 14, 2015 at 17:48
  • @SethMMorton that's not what he wants. the while loop is just to keep asking for a new value until a valid one is provided Commented Nov 14, 2015 at 17:58
  • If my answer solves your problem please accept it. If it doesn't let me know what is still the problem and I'll try to help you with it Commented Nov 14, 2015 at 19:20

1 Answer 1

4

You want choice to be either "temp" or "distance", so your while condition should be that it can't (not be "temp" AND not be "distance"). Just substitute the or for and on the while condition.

prompt = "> "

print "Welcome to the converter. What would you like \
to convert? (temp or distance)"
choice = raw_input(prompt)

while (choice != "temp" and choice != "distance"):
    print "Sorry, that's not an option"
    choice = raw_input(prompt)
if choice == "temp":
    print "temp"
elif choice == "distance":
    print "distance"

The way you had it before the condition would always be true

Per suggestions below other ways you could write the while condition that would also work:

while not (choice == "temp" or choice == "distance"):

or

while (choice not in ('temp', 'distance')):

Take your pick.

Sign up to request clarification or add additional context in comments.

4 Comments

Psst: DeMorgan's laws would go a long way to making that conditional more readable.
And, of course, another option is choice not in ('temp', 'distance')
@dietbacon, thank you! That makes sense - I basically created an infinite loop with the while statement because no matter what choice would either be "not distance" or "not temp". You explained it very well. I changed the "or" to "and" and everything works as expected now.
@MasterModnar no problem, thats what we're here for :)

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.