Coffee Chatbot

Could anyone provide me some insight on this error.
When I run the code in vscode it works as expected but in code academy I seem to get this error regardless of the letter of input ( a, b, c)

Thank you

def get_size():
   res = input("What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ")
   if res == "a":
     return 'small'
   elif res == 'b':
     return 'medium'
   elif res == 'c':
     return 'large'
   

def coffee_bot():
  print("Welcome to the cafe!")
  size = get_size()
  print("your size is " + str(size))

coffee_bot()
Traceback (most recent call last):
  File "script.py", line 18, in <module>
    coffee_bot()
  File "script.py", line 15, in coffee_bot
    size = get_size()
  File "script.py", line 2, in get_size
    res = input("What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ")
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

That’s weird. This is in the Python shell…

>>> res = input("What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ")
What size drink can I get for you? 
[a] Small 
[b] Medium 
[c] Large 
> a
>>> res = input('''
What size drink can I get for you?
[a] Small
[b] Medium
[c] Large
> ''')

What size drink can I get for you?
[a] Small
[b] Medium
[c] Large
> a
>>> 

Yea @mtf that’s exactly what stumps me
So I can not progress because of this

** Update **

So I believe I found the error this morning. I was running the script with


python script.py

which was creating the error

when I run the script with


python3 script.py 

I no longer get the name space error

Hopefully this helps someone else :slight_smile:

2 Likes

Same problem here re python3 - I was lucky enough to figure that one out on my own. Took me a while though.

I do have a follow up question on this lesson though.

def print_message(): print('I\'m sorry, I did not understand your selection. Please enter the corresponding letter for your response.') def get_size(): res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ') if res == 'a': return 'small' elif res == 'b': return 'medium' elif res == 'c': return 'large' else: print_message() return get_size() def get_drink_type(): res = input('What type of drink would you like? \n[a] Brewed Coffee \n[b] Mocha \n[c] Latte \n> ') if res == 'a': return 'Brewed Coffee' elif res == 'b': return 'Mocha' elif res == 'c': return order_latte() else: print_message() return get_drink_type() def order_latte(): res = input('And what kind of milk for your latte? \n[a] B2% milk \n[b] Non-fat milk \n[c] Soy milk \n> ') if res == 'a': return '2% milk' elif res == 'b': return 'Non-fat milk' elif res == 'c': return 'Soy milk' else: print_message() return order_latte() def coffee_bot(): print('Welcome to the cafe!') size = get_size() # print(size) drink_type = get_drink_type() # print(drink_type) milk = order_latte() # print(milk) print('Alright, that\'s a {} {}!'.format(size, drink_type)) name = input("Can I get your name please? ") print("Thanks, " + name + "! Your drink will be ready shortly.") # Call coffee_bot()! coffee_bot()

If I run this and choose c (Latte) for my second question, I get the milk question returned but then I get the question again. I can see why this is happening, butI would prefer it not to get asked a second time. I have copied the hints and continue to have the problem.