Looping Coffee Chatbot

In this exercise we are building a coffee chatbot. I have managed to complete all tasks, but for the bonus task I do not know how I would achieve the following:

‘Allow the chatbot to recognize key exit words such as 'stop' or 'bye' that can terminate the order at any step’

How do I add a code that recognizes stop, bye, quit or whatever else as an input and stops the bot?

Thanks
Bruna

It shouldn’t take too much adaptation from your original code. You’re already performing most of the necessary logic steps so you just need to add a few more lines to meet this new requirement.

You’re looking to check if a statement is equivalent to ‘stop’ or ‘bye’. I’m assuming you keep your program running by virtue of a loop. You’re looking to break out of that loop under the given circumstances?
Some hints there.

Hello @brunarotzschthomas69!! Welcome to the forums :grinning:

If your program runs on a single loop, than @tgrtim is spot on.

I would like to add that if you need to stop a program from any point with out having to stop multiple loops you can use sys.exit()

import sys

#Imagine a bunch of very complicated code doing very important things

#check for a condition
command = input('enter a command')
if command = 'stop':
    sys.exit()

I just recently learned about the system module, and have found it to be very useful.

Hi y’all!
Expanding on what Bruna asked:
Is writing if statements every step of the way within the coffee chatbot the only way to accomplish this?
For example:

def get_size():
    stopWords = ['stop', 'Stop', 'cancel', 'Cancel', 'quit', 'Quit']
    while True:
        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'
        elif res in stopWords:
            print('Order cancelled!')

And then insert similar elif statements for all the inner functions (get_drink_type(), order_latte(), etc.).

Or is it possible to write a specific yet broader function that sort of “runs in the background” while the bot is being executed so it could detect if the user inputs any of the “stop words”?

The only idea I have had for this up to this point is to write another function - within which I would then house the coffee chatbot - and somehow leverage a boolian and a while loop so it would, as I mentioned, run “in the background” and get activated if any of the stop words were inputted; however, I have not figured out how to actually write the darn thing.

Any and all help would be much appreciated.

Hi @robotoeuf

See https://www.onlinegdb.com/HkK1jPoPU?fbclid=IwAR0EuU-VeDQ-CQBBH8t0DUTOShEc9C-gY9EddAk-Sl3kxE6ZwrAD9jH2MLY

Maybe it helps.

best regards

Maxwell