Coffee Bot python challenge problem

Hello,
I am doing the coffee bot challenge in python (https://www.codecademy.com/paths/build-chatbots-with-python/tracks/introduction-to-python-and-chatbots/modules/chatbot-evolution-and-dialog-trees/projects/coffee-chatbot) and i am stuck. I am just a beginner and i need help. I’ve written the following code but when i run it, i get the following error:
How can i fix this please help
My code:

Define your functions

def coffee_bot():

print(“Welcome to the cafe!”)

size = get_size()

print(size)

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:

return get_size()

Call coffee_bot()!

coffee_bot()

Error/Output message:

Welcome to the cafe!
What size drink can I get for you?
[a] Small
[b] Medium
[c] Large

a
Traceback (most recent call last):
File “script.py”, line 20, in
coffee_bot()
File “script.py”, line 4, in coffee_bot
size = get_size()
File “script.py”, line 8, in get_size
res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ')
File “”, line 1, in
NameError: name ‘a’ is not defined

Hi,

I believe the problem is just that you are using the wrong quote marks within the if statements.

if(res==‘a’):

You should be using " or ’ instead.

if(res=="a"):

Then your code works fine for me.

Hope that helps!

the line size = get_size() is giving you an error
because
you called the function get_size before you defined it

Putting the function decalration for get_size() before the function declaration for coffee_bot() should fix that problem.

(Python is one of the languages where you have to declare a function before you call it.)

1 Like

This does not work! Despite putting my get_size() function above the coffee_bot() function it still throws the same error

Did not work. In python aren’t both “” and ‘’ considered as strings

I removed the input from the code
and I removed the recursive function call ( get_size() being called inside get_size() )
The remaining code seems to work, so the problem is probably with one of those.

def get_size(): res = 'c' #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: #return get_size() return 'medium' def coffee_bot(): print("Welcome to the cafe!") size = get_size() print(size) coffee_bot()

If we look closely at this, there is nothing in the call stack, yet. This recursion should ultimately wind down to and return a valid input. Worth leaving in to test and prove it works, or doesn’t work.

This is a problem that can be broken down, as has been done in your code. Does get_size() need to be a global object, though?

A fair compromise would be to have it in the function scope of coffee_bot().

def coffee_bot():
    prompt = "What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> "
    def get_size():
        return {'a':'Small', 'b':'Medium', 'c':'Large'}.get(input(prompt), 'Medium')
    return f"Your {get_size()} coffee is served."

print (coffee_bot())
What size drink can I get for you? 
[a] Small 
[b] Medium 
[c] Large 
> d
Your Medium coffee is served.
>>> 
What size drink can I get for you? 
[a] Small 
[b] Medium 
[c] Large 
> c
Your Large coffee is served.
>>> 

i dont really know that much of python so can you please simplify your solution code. Besides, i dontunderstand what the problem really is. My code should work fine too right?? i keep calling my function until the user gives a valid input and then calls are subsequently popped from the call stack and the valid answer is passed down the call stack until the very end. Why does this not work?

I think you mean expand upon since it is simplified.


In the above example we don’t use recursion, though it would work. It is, imho, overkill.

Instead, we use a default value set in the dict.get() method call. If a valid key is not input, then the default return will be 'Medium'.

One wonders whether the recursion approach applies in this particular instance. There is some supreme elegance in this approach when it actually applies. One should not discard it. However, is it the approach for the current situation?

it says in the instructions that you’ve to use recursion and anyway even when i run my code without recursion, it throws the same error. That is, even when my code is as follows, the same error is thrown.

Define your functions

def get_size():

res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ')

return res

def coffee_bot():

print(“Welcome to the cafe!”)

size = get_size()

Call coffee_bot()!

coffee_bot()

Keep working on it. I’ve got to bolt or fall out of this chair. Night’s upon us. Cheers!

Hey Arc0872089957,

According to the Python Docs, EOFError are raised when the input() function hits an end-of-file condition (EOF) without reading any data. According to Geek4Geeks, this error is sometimes experienced while using online IDEs…

So good news, there maybe something wrong with the Codebyte application and not your code!
It seems like Codebytes doesn’t like the input() function. The following code gets an EOFError.

user_input = input('Enter something') print(user_input )

Traceback (most recent call last):
File “main.py”, line 1, in
user_input = input(‘Enter something’)
EOFError: EOF when reading a line

Geek4Geeks recommends using a Try Except Statement to overcome this issue:

try: user_input = input('Enter something: ') print(user_input) except EOFError as e: print(e)

But as you can see, this suppresses the Traceback error, but the input() function still hits an EOF condition without reading any data. So again, it there maybe a problem with the Codebyte application and not your code!
I hope this helps!

Thank you so much, now it makes sense. However even when i tried to run it on my IDE (Visual Studio Code), it did not work.