Coffee Chatbot : NameError order_latte is not defined

Hello. I have a little problem with this exercise. I dont understand where is a mistake.
i have a NameError: order_latte is not defined. Maybe someone can help me. Thank you for help.

def  coffee_bot():
  print("Welcome to the cafe!")
coffee_bot()

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'
  if res == 'b':
    return 'medium'
  if res == 'c':
    return 'large'
  else:
    print_message()
    return get_size()
    
size = get_size()
print(size)   

def get_drink_type():
  res = input("What type of drink would you like? \n[a] Brewed Coffe \n[b] Mocha \n[c] Latte \n> ")
  if res == 'a':
    return "Brewed Coffe"
  if res == 'b':
    return 'Mocha'
  if res == 'c':
    return order_latte()
  else:
    print_message()
    return get_drink_type()
    
drink_type = get_drink_type()
print(drink_type)
  
def order_latte():
  res = input("What kind of milk for your latte? \n[a] 2% milk \n[b] Non-fat milk \n[c] Soy milk \n> ")
  if res == 'a':
    return '2% milk'
  if res == 'b':
    return 'Non-fat milk'
  if res == 'c':
    return 'Soy milk'
  else:
    print_message()
    return order_latte()
    
latte_options = order_latte()
print(latte_options)

when I use answer c in the second step this error occurs

Hi there, welcome to the forums.

It’s because Python is an interpreted language, so when you run your program Python executes it line-by-line, top to bottom.

In your code, by the time you get to doing

if res == 'c':
    return order_latte()

you haven’t yet declared the function order_latte(), so Python doesn’t know what you mean. :slight_smile:

Also, I formatted your code correctly so we can see the indenting. Please read this thread to see how to do this for future questions. (It makes it a lot easier for us to help you, especially with Python! :slight_smile: )

great thank you for help :slight_smile:

1 Like

tries to declare caffe_latte () before it appears in the get_drink_type () function, however, despite all attempts, NameError messages still appear. Could you show me exactly what it should look like? I will be very grateful.

1 Like

You should be able to simply move the entire order_latte function above get_drink_type and it should work. (At least, it did when I tried that in a repl…) :slight_smile:

You don’t declare things.
When you execute a def statement, you create a function and assign that name to it, it is an action carried out.
So you would create all the functions you need, then invoke them. Doesn’t matter what order they are created in, you just need to create them before you end up calling them.

def a():
    b()

def b():
  return 0

a()  # a and b both exist at this time
1 Like