Build Coffee bot using python (Module 1)

Define your functions

def coffee_bot():
print(“Welcome to the cafe!”)

size = get_size()
print(size)

drink_type = get_drink_type()
print(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 get_drink_type():
res = input(“What type of drink would you like? \n[a] Brewed Coffee \n[b] Mocha \n[c] Latte \n>”)
return res
if res == ‘a’:
return ‘brewed coffee’
elif res == ‘b’:
return ‘mocha’
elif res == ‘c’:
return order_latte()
else:
print_message()
return get_drink_type()

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 ‘latte’
elif res == ‘b’:
return ‘non-fat latte’
elif res == ‘c’:
return ‘soy latte’
else:
print_message()
return order_latte()

coffee_bot()

The above code is supposed to display the drink type with its corresponding name in the output. Also, if the user selects latte, then the code should move to order_latte() function. But this is not happening. I cant figure out what it is. Please help me to point out my mistake.

I think the code has a semantic error.

I think, the structure should be:

def print_message():
     #Code
def get_size():
     #Code
def order_latte():
     #Code
def get_drink_type():
     #Code
def coffee_bot():
     #Code

Hi @er.bhaskar95. When submitting code to the forum please follow the guide at the following which allows clear formatting-

It’s hard to be certain without indentation but I’d double check the get_drink_type function. What’s different about the execution of this function compared to the others?

I am extremely sorry for the code indentation problem @tgrtim . I will keep these guidelines in mind from now onwards.

The problem with get_drink_type function is that it should be printing the name of the drink (either brewed coffee or mocha or latte) when the corresponding letter is entered by the user. But in this case, it doesn’t do so. it simply returns the letter that i press. Also, when latte is selected (letter b), the code should jump to another function i.e order_latte() but it doesn’t happen. The code just terminates after drink_type function.

I am not getting any sort of error in the code like indentations or syntax error etc.

Is somehow order of those functions related like @snikhill believes? I tried changing order too, but its still the same.

No worries, it’s just easier to debug especially in a language like Python where indentation isn’t a guideline but a requirement.

You’ve actually described your problem exactly. Why does this function return the letter you press rather than a specific string?

You have written return res in get_drink_type() . The return keyword ends further working of the function and hence, your code stops.

That is the problem with this code i.e returning the same letter and terminating the code after get_drink_type function.

Will play with this code a bit more and try to figure out whats wrong here. Meanwhile, if you have suggestions to try out, please share. I can test it.

I used return res in get_size() function too but it doesn’t stop. Infact, it works perfectly fine and completes the get_size task. After that, its behaving abnormally.

Statments in a specific code block are executed are executed in a linear order. Is the order the same for these two functions? Why might they act differently?

Yes, statements are executed in a linear order. But, the return keyword marks the end of the function and hence, function’s execution is stopped.

According to the code you have attached, you are not using: return res in get_size() function.

You are only using return res in get_drink_type(). Remove this.

Thanks a lot @snikhill. This worked. That return return res in get_drink_type() actually terminated my code. Now I know, why it was behaving abnormally.

Yes @tgrtim. I first broke my code into smaller parts and was able to spot that faulty order in my code. Thanks a lot for helping me out with this :slight_smile: