Looping Coffee Chatbot

I finished the main code, but want to complete the bonus task. I stuck with adding additional inputs besides 'y' and 'n' – such as 'yes' , 'sure' , or 'nah' – when it asks the user if they would like another drink. Please help

from utils import print_message, get_size, order_latte

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")

  if order_drink in ["y", "n"]:  

    break

print(“Okay, so I have:”)

for list_of_drinks in drinks:

print("-", list_of_drinks)       

name = input('Can I get your name please? \n> ')

print(‘Thanks, {}! Your order will be ready shortly.’.format(name))

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 order_mocha()

elif res == ‘c’:

return order_latte()

else:

print_message()

return get_drink_type()

Define new functions here!

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()

You can do this in a couple of different ways. You have the option of accounting for all the variations, so making the while loop terminate if order_drink is any of the negative responses. So this would be for example:

while order_drink == 'y' | order_drink == 'yeah' | order_drink == 'yep':
   # some code here
   while True:
      order_drink = input('Would you like to order another drink? (y/n)\n')
      if order_drink in ["y", "yeah", "yep", "n"]:
         break

The other option is to coerce any of the valid inputs into a ‘y’ or ‘n’. So for example this would be:

   while True:
      order_drink = input('Would you like to order another drink? (y/n)\n')
      if order_drink in ["Y", "Yeah", "yeah", "yep", "sure", "Sure"]:
         order_drink = 'y'
      elif order_drink in ["N", "no", "No", "nope", "nah", "no thanks"]:
         order_drink = 'n'
      if order_drink in ["y", "n"]:
         break

and the rest of the code would stay exactly the same as order_drink is being coerced into one of the two correct phrases. It’s up to you how you would prefer to proceed with that, they’re both valid options that should work.

1 Like

thank you so much
you helped me