Why is it that using elif instead of if with the following function returns "none" instead of the chosen size?

https://www.codecademy.com/paths/build-chatbots-with-python/tracks/introduction-to-python-and-chatbots/modules/chatbot-evolution-and-dialog-trees/projects/coffee-chatbot

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_error()
    get_size()

This function returns “none” when choosing a valid size. If I change the middle two blocks of code from ‘elif’ to ‘if,’ the issue is fixed. Why is this?

1 Like

Form the code you’ve provided (if -> elif -> elif -> else) it all seems to work as expected when I tried it-

>a
Out: 'small'
>b
Out: 'medium'
>c
Out: 'large'

I’m not certain what issue you’ve enocuntered that would make this code perform otherwise :confused:
Apologies if that’s not too hlepful but the logic seems sound.

1 Like

Here’s the whole thing, if it helps.

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'
  if res == 'b':
    return 'medium'
  if res == 'c':
    return 'large'
  else:
    print_error()
    get_size()

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

# Call coffee_bot()!
coffee_bot()
1 Like

I’ve tried it with both if statements only and an if, elif chain and I get the correct answer when entering a, b or c to the terminal. This is on my own interpreter though.

I’m wondering if there is perhaps an issue with using input. You’ll come across a couple of issues on the forums when using input with old sessions not being cleaned up. I’d consider clearing your browser cache to see if this affects the results at all. If not, hopefully someone else has a bright idea.

1 Like

Hi there.

I can break your program even with each branch existing as a stand-alone if.

The problem is I think being caused by your recursive call to get_size(). If an invalid entry is given the first time - for example, someone providing an uppercase A when you’re expecting only lowercase responses - this causes the program to execute the else block.

It then re-runs get_size() - a recursive call, as the function is calling itself - but the return value from that recursive run is not captured. Subsequently, a valid second attempt will correctly return a value, but this is discarded immediately. Lacking a defined return value, Python will just return “None” from the end of executing the function.

3 Likes

Thanks for your response.

1 Like

No problem. :slight_smile:

If you need help with anything else, you know where we are. :slight_smile:

1 Like

I have this super misleading haskell snippet where that does the right thing.

the misleading part is that all 4 of the case branches are returned, and return itself, isn’t a return statement or anything like that

main = askSize >>= print

askSize = do
  putStrLn "What size drink can I get for you?"
  putStrLn "[a] Small"
  putStrLn "[b] Medium"
  putStrLn "[c] Large"
  getLine >>= \case
    "a" -> return "small"
    "b" -> return "medium"
    "c" -> return "large"
    _   -> askSize
What size drink can I get for you?
[a] Small
[b] Medium
[c] Large
n
What size drink can I get for you?
[a] Small
[b] Medium
[c] Large
n
What size drink can I get for you?
[a] Small
[b] Medium
[c] Large
a
"small"
1 Like