# Question about recursion in chatbot learning path

I’m currently working my way through the chatbot path after completing the Python3 path. On the coffee chatbot project it asks to take a size for a drink, and if an invalid response is given to run the get_size() function again. This is the code I have for it, that does appear to be correct based on the hints given at the side:

``````def coffee_bot():
print('Welcome to the cafe!')
size = get_size()
print(size)

def get_size():
res = input("What size drink can I get 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:
get_size()
``````

The problem is when the recursion is used. If a valid answer is given immediately, it will return the correct size (i.e. small, medium or large). However, if an invalid letter is given and the function runs again, any later, including a b c is returning None. I can’t figure out why the is so would look any advice! There is nothing in the steps or hints that suggests this should be happening!

when there is invalid input, this code execute:

``````  else:
get_size()
``````

so then the function execute again. If you then enter something valid the right answer is returned, which means we are now back here:

``````    get_size()
``````

but then you forgot to `return` what you got back from the recursion, you need to do:

``````  else:
return get_size()
``````

otherwise you don’t return what get_size() gives back

how to explain this? I would highly recommend using this tool:

http://www.pythontutor.com/visualize.html#mode=edit

or something similar, it allows you to step through your code. Really helps with understanding the recursion.

also, this topic is a good read:

Recursive function calls

maybe not related to the exercise you work on, but contains some good examples of recursion.

1 Like

Argh I should have known that! Silly mistake! Thank you

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.