Python coffee chatbot

# Define your functions
def coffee_bot():
  print("Welcome to the cafe!")
  size = get_size()
  print(size)

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'

  
# Call coffee_bot()!
coffee_bot()

when I enter an input (say ,a) then it gives me an error
Traceback (most recent call last):
File “script.py”, line 20, in
coffee_bot()
File “script.py”, line 4, in coffee_bot
size = get_size()
File “script.py”, line 8, in get_size
res = input("What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ")
File “”, line 1, in
NameError: name ‘a’ is not defined

Someone please help me!

I tried your code on my local env but I didn’t got any errors. Can you please share the screenshot of when ou get this error (Including the image of your code and error displayed in terminal on website).
I will only be able to assist you further after that.
Thanks.

2 Likes

Dude literally I don’t know what’s the problem here because I ran this code both at codecademy and offline on my computer but it’s working completely fine. One thing I notice here is return statements in both elif are a little more indented, but that’s definitely not the problem which is causing this error. I suggest you report bug in here. This might be some internal problem with the online code editor at codecademy.

This seems likely to be an issue with Python2 compared to Python3. Note that the function input() differs between the two-



If you’re limited to using Python2 consider using the raw_input() function when dealing with user input.
3 Likes

This could be a possible problem but in this project Python 3 is used. I’ve done this project in Build Chatbots with Python skill path.

Some of these projects appear to use Python 3, however they actually compile using Python 2. In Python 2, input() is still a function, however it does not automatically convert to string like raw_input() does. Here is some reading on the matter.

3 Likes

In that case, this would be the most appropriate reason for this error. But why didn’t this happen with anyone else? :thinking: :thinking:

This is happening because they are using python script.py in the bash terminal instead of python3 script.py. Try running with with python3 instead and you shouldn’t get any errors.

3 Likes

yes actually i was styping python script.py in the terminal I didn’t knew that you have to explicitly define python3.
Thanks everybody

You’ll have to specify python3 in the bash terminal at codecademy. But python will work in terminal of your pc.

Good you got the solution!

1 Like