0

I am experiencing unwanted behavior with my code's if statements. When I call conditional statements inside the while loop, only the first condition is called no matter which input is supplied. I wish for the proper input to call the properly described function and then continue back to the beginning of the loop to ask the user for another choice.

color_modes = ['sangria', 'ham', 'nightHawk']
print color_modes
def sangria():
    my_range = list(range(20))
    print my_range
def ham():
    print 'foo'
def nightHawk():
    print 'nightHawk'
while True:
    user_input = input('...')
    if 'sangria':
        ham()
        continue
    if 'ham':
        sangria()
        continue
    if 'nightHawk':
        nightHawk()
        continue
    else:
        break

Moreoever, when I use the syntax:

if user_input == 'ham': instead of the shorthand if 'ham:, the condition does not function. Thank you in advance.

0

3 Answers 3

1

In Python 2

user_input = input('...')

attempts to evaluate the user's input. If you input a string such as ham Python will try to evaluate the expression ham which will fail as shown below (unless there is a similarly named variable in scope).

>>> input('...')
...ham
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'ham' is not defined

You could fix it by using raw_input() instead:

user_input = raw_input('...')

and then using string comparison:

while True:
    user_input = raw_input('...')
    if user_input == 'ham':
        ham()
    elif user_input == 'sangria':
        sangria()
    elif user_input == 'nightHawk':
        nightHawk()
    else:
        break

This code also uses elif to avoid the unnecessary continue statements.

A better way is to use a dictionary to map the user input to the associated function:

input_map = {'ham': ham, 'sangria': sangria, 'nightHawk': nightHawk}
while True:
    func = input_map.get(raw_input('...'))
    if func is None:
        break
    func()
Sign up to request clarification or add additional context in comments.

Comments

1

if 'sangria': is only shorthand for if bool('sangria'):, which always evaluates to if True, therefore the first thing is ran regardless of input

You need to compare the input string. There's no shorthand / alternative way to write it like a Java switch case statement

This code looks odd, by the way.

if 'sangria':
    ham()
    continue
if 'ham':
    sangria()
    continue

If I understand what you want, you can create a function map.

funcs = {"ham": ham, "sangria" : sangria, "nightHawk" : nightHawk} 
user_input = input() 
funcs[user_input]() # use the string to get the function object, then call it 

Comments

1

if 'ham' will always pass since an not empty string is always True.

Instead, you must check for equality using the == operator.

The correct code would be:

color_modes = ['sangria', 'ham', 'nightHawk']
print color_modes
def sangria():
    my_range = list(range(20))
    print my_range
def ham():
    print 'foo'
def nightHawk():
    print 'nightHawk'
while True:
    user_input = input('...')
    if user_input == 'sangria':
        ham()
        continue
    if user_input == 'ham':
        sangria()
        continue
    if user_input == 'nightHawk':
        nightHawk()
        continue
    else:
        break

1 Comment

Thank you for the reply. i understand now that raw_input in 2.7 and input in python 3 return strings, but input by default in 2.7 returns an expression. I suppose it was this simple error on my part. Thank you to all for clarifiying. Thank you as well for clarifying what if 'somestring' does.

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.