Python Data Structures and Loops: Looping coffee chatbot: how to introduce "stop" or "bye" into code

In Bonus Task 13 it is advised to make the coffee bot able to recognize key words “stop” or “bye” at any step of ordering coffee. I´ve done a bit of research, but am still stuck. What I´ve found out so far (just a hunch) that it must be connected to breaking the while-loop, but at what step and how exactly, remains a mystery to me. The code itself is as follows: (I´ve copied the functions from utils.py into the script.py)


def coffee_bot():
  print('Welcome to the cafe!')
  order_drink = "y"
  drinks = []
  while order_drink == "y":
    size = get_size()  
    drink_type = get_drink_type()

    drink = '{} {}'.format(size, drink_type)
    print('Alright, that\'s a {}!'.format(drink))
    drinks.append(drink)
    while True:
      order_drink = input("Would you like to order another drink? \n[a] y \n[b] n \n>")
      if order_drink == "y" or "n" or "yes" or "sure" or "no":
        break      
      print('Okay, so I have:')
    for drink in drinks:
      print('-', drink)    
    
  name = input('Can I get your name please? \n> ')
  print('Thanks, {}! Your order will be ready shortly.'.format(name))
  coffee_bot()
      

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 order_brewed_coffee()
  elif res == 'b':
    return order_mocha()
  elif res == 'c':
    return order_latte()
  else:
    print_message()
    return get_drink_type()

  
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 order_latte():
  res = input('And 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 'ordinary latte with 2% milk'
  elif res == 'b':
    return 'non-fat latte'
  elif res == 'c':
    return 'soy latte'
  else:
    print_message()
    return order_latte()
  
# Define new functions here!
def order_brewed_coffee():
  while True:
    res = input("Would you like to try coffee brewed from the finest Brazilian grounds? \n[a] Yes! \n[b] No way, Mister! \n>")
    if res == "a":
      return "Brazilian grounds coffee"
    elif res == "b":
      return "ordinary brewed coffee"
    print_message()

def order_mocha():
  while True:
    res = input("Would you like to try our limited-edition peppermint mocha? \n[a] Sure! \n[b] Maybe next time! \n>")
    if res == "a":
      return "peppermint mocha"
    elif res == "b":
      return "mocha"
    print_message()
    
coffee_bot()

This may not be the most elegant way of doing it but the way I achieved it was making a list with terms, checking during if statements if the user had input them, and then exiting if so. For example:

terminate = ['bye', 'exit', 'quit', 'stop', 'leave']
...
elif res in terminate:
  exit()

I added the terminate list outside of the functions and then used the elif statement in the body of each function before the else statements.

Thank You for Your reply. I have already solved the case thanks to a friend of mine, but Your solution seems also good. :wink:

Would you mind sharing the solution that your friend gave you?

# Returns user input or returns to the beginning if the input is "stop" (order is cancelled).
def order_input(text):
    res = input(text)
    if res == "stop":
        print("Order cancelled!")
        coffee_bot()
    else:
        return res

Hi @maxwell17

I’m trying to implement the function that would stop the coffee bot if the user typed in any of the ‘stop words’, at any given time, but cannot seem to make it work (other than doing something similar to what @micro2560755154 mentioned, i. e. writing if statements within every step of the way). How does this snippet you mentioned work? Did you define it within the coffee_bot() function? Where is it called?
Thanks in advance!

Best thing to do for that is … whereever you have used print_message() , make it print_message(res) . and make your defination of print_message like :

def print_message(res):

terminator = [‘stop’,‘bye’]

if res in terminator:

print("Order Cancelled")

exit()

else:

print('I\'m sorry, I did not understand your selection. Please enter the corresponding letter for your response.')

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.