Coffee Chatbot - Multiple Answers - Python Lists in conjunction with loops and inputs

I’m trying to anticipate a user input based on a variety of yes or no answers for the Coffee Chatbot: Loops assignment:

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

  order_drink = ['y', 'yes', 'sure', 'ok', 'yeah', 'yup']
  drinks = []
  for answer in order_drink:
    if answer == 'y' or 'yes' or 'sure' or 'ok' or 'yeah' or 'yup':
      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> ')
        for answer in order_drink:
          if order_drink == 'no' or 'n' or 'nope' or 'nah':
            break

Basically if a person inputs yes or yeah, I would like it to yield True based on a list of anticipated yes words. Thanks for your help.

1 Like

Hello @chip6158055161, if I understand you correctly, you are asking why it isn’t working? The reason is this:

You can’t have

if answer == condition1 or condition2:

You must provide what you want to be compared to the condition each time:

if answer == condition1 or answer == condition2:

Notice how I specify the answer variable both times before I compare it to the condition?

Also here:

Isn’t necessary. You are saying: For every element of the list order_drink, if that element equals an element in order_drink, execute the following code. But that isn’t required because everything in order_drink is already in order_drink. Also, the code will replace all of the elements of order_drink with the user’s input on this line:

That will change order_drink into a string containing the user’s input. You don’t need to iterate over it here:

because the list order_drink has now become a string. If you iterate over a string, the loop iterates over each character:

var1 = "hi"
for i in var1:
  print(i)
#This will print:
#h
#i

I hope this helps!

Thank you very much. This definitely exposed a lot of issues with my attempt. But, I guess I’m still struggling to find a solution.

This is what I started with:

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

  order_drink = 'y'

  while order_drink == 'y':
    size = get_size()  
    drink_type = get_drink_type()

    drink = '{} {}'.format(size, drink_type)
    print('Alright, that\'s a {}!'.format(drink))

    while True:
      order_drink = input('Would you like to order another drink? (y/n) \n> ')

      if order_drink in ['y', 'n']:
        break

As of right now, the loop only works when it yields a ‘y’ or ‘n’ from input. How do I make this chatbot work with varying type of anticipated yes or no answers? Every time I try to employ a list of yes or no type answers the loop no longer breaks. Thank you for your help.

Hello @chip6158055161, you could use something like:

yes_answers = ["yes", "yeah"] #etc
answer = input(">>")
while answer in yes_answers:
  #code   
  answer = input(">>")
else:
  #code
  break

I will explain this.
The yes_answer list contains a list of answers that would mean yes.
You then get the user input, answer.
Then the while loop checks to see if the answer is positive:

while answer in yes_answers:

This basically checks to see if the string, integer, etc answer is in the list yes_answers
While the above condition is true the chatbot code should execute (i.e , “size, type, etc.”).
Then, the last line of the while loop asks for the user input. That way, the user can ask for another drink, and it avoids an infinite loop.

The else performs code that will happen if the while condition is false. (If the user provides an answer not in the yes_answers list.)
In here, you could tell the user what drinks they have, and then break.

How I did it. Don't look at it unless you are absolutely stuck. Comes with explanation:
def coffee():#Defines a function
  print("Welcome")#Prints the welcome message
  positive_words = ['y', 'yes', 'sure', 'ok', 'yeah', 'yup']#List containing words that the program will interpret as positive
  drinks = []#An empty list that will contain the drinks the customer orders
  answer = input("Do you want a drink?>")#Gets the user input; do they want a drink.
  while answer in positive_words:#This is a while loop. Like I described above.
#It checks to see is the user's answer is within the positive words list. If so, this code executes, and will
#do unless the user provides a negative answer:
    size = get_size()#Calls the get_size function. Stores to a variable.
    drink_type = get_drink_type()#Calls the get_drink function. Stores to a variable.
    drink = '{} {}'.format(size, drink_type)#Size and drink together
    print('Alright, that\'s a {}!'.format(drink))#Prints what drink the user just ordered
    drinks.append(drink)#Adds that drink to the list that contains the user's drinks
    answer = input("another drink?>")#This is crucial. It asks the user if they want another drink.
    #If they answer an answer within the positive_words, the loop will cycle again.
    #It will continue to cycle until the loop condition is no longer met.
  else:#If the loop condition is no longer met (the user provides an answer not in positive_words, the following code with run:
    print(drinks)#This prints the drinks list, so the user can see what drinks they ordered.

You might notice I didn’t have a break in the else part of the loop. That is because the else of a while loop doesn’t need a break. It will run its code, and then exit, without the need for a break:

a = 5
while a > 3:
  print(a)
  a -= 1
else:
  print("It is now 3")

print("Not in the loop")

Will print:

>>5
>>4
>>It is now 3
>>Not in the loop

Due to the fact that the loop exits once the else code is executed.

I hope this helps!

5 Likes

Thank you so much! This helped plenty. I was in-fact stuck. But I think I’m starting to get it and now I’m back to having fun. Thanks for all your help!

1 Like

You’re most welcome! Always remember that if you’re stuck, it does wonders to take a break, even go for a walk, and then come back the next day. Also don’t feel afraid to re-visit topics that you’ve already done! I’m glad I could help!