Odd error

Hello, I’m having some odd errors with this input code. what am I missing?

# Define your functions res = "f" def coffee_bot(): print("Welcome to the cafe!") size = get_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") # Call coffee_bot()! coffee_bot() print(size)

Thanks!
-Chuck

Note that input( ) does not work in Codebytes and it does not work in many of the CodeCademy lessons.

Other than that, the coffee_bot function uses a get_size() so
the get_size may have to be before coffee_bot
(I think that Python doesn’t do hoisting for variables.)

Also, you’re trying to create the size variable inside of the coffee_bot function and use it outside that function …
but creating the size variable inside of the coffee_bot function would make size have the wrong scope (meaning it would only work inside that coffee_bot function).

The workaround is to force the variable to have global scope by using the global keyword:

def coffee_bot():
  print("Welcome to the cafe!")
  global size
  size = get_size()

Must comment, this is neither a ‘workaround’ nor ‘force’. The variable has global scope. Our function can see it (read binding) in that scope. It just cannot update it because it has no, write binding. The global keyword simply defines local variables with global bindings. Now we can write to or update the values.

Bindings are akin to accessors in programming terms.

1 Like

I’m going to clarify that this is for the coffee chatbot lesson in the intro to python and chatbots and this is per instructions in the lesson.

Ok, thanks (the lesson says to call the get size function in the coffee bot function but that works) one note: is the res up top needed?
-Chuck

whelp that’s embarrassing.

That is the bad line. That needs to be just a function call. Whoops! :frowning:

I was trying to use a variable declared in a function and call that variable out of the function.
-Chuck