Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Shopping List Redux

Philip Finch
Philip Finch
5,080 Points

Why not end the if block with an else?

I notice Kenneth doesn't end the if block with an else. Functionally i don't see any difference between the following two blocks of code. Is there a benefit either to using or not using an else statement to put the add_to_list(new_item) function in at the end?

his code:

while True:
  new_item = input("> ")
  if new_item == "DONE":
    break
  elif new_item == "HELP":
    show_help()
    continue
  elif new_item == "SHOW":
    show_list()
    continue

  add_to_list(new_item)
  continue

my code:

while True:
  new_item = input("> ")
  if new_item == "DONE":
    break
  elif new_item == "HELP":
    show_help()
    continue
  elif new_item == "SHOW":
    show_list()
    continue
  else:  
    add_to_list(new_item)
    continue

2 Answers

This segment of the code:

else: 
    add_to_list(new_item)
    continue

will only run if none of the other 3 requirements are met, hence "else" implying that if and elif requirements aren't met. We want anything that is inputted to be added new_item therefore we put it outside the if-else block of code. This implies that regardless to whether the conditions of the if and elifs are met, the input will be added to new_item

... and the add_to_list(new_item) can potentially be run multiple times because it's part of the loop. Whereas, in the "his code", it will only be run once.

Philip Finch
Philip Finch
5,080 Points

Dor Rondel : We don't want all inputs to be added to new_item. We don't want "DONE", "HELP", or "SHOW" added to new_item, and it seems both versions function the exact same way because there's a break or continue after each if/elif.

Arthur The Dog : In his code the add_to_list(new_item) is still part of the loop, it's just dropped down an extra line.

Nicolas Hampton
Nicolas Hampton
44,625 Points

What you're looking for (I was actually looking myself, because I had the same thought, but forgot to put the colon after my else statement) is this:

show_help()

while True:
  new_item = input("> ")
  if new_item == "DONE":
    break
  elif new_item == 'HELP':
    show_help()
  elif new_item == 'SHOW':
    show_list()
  else:
    add_to_list(new_item)

show_list()

This structure using else allows us to test for all the different commands that we want to accept. If and only if all the commands are not present, then we'll add the item to the list, thus avoiding adding commands to the list. Putting the add_to_list function in a 'catch everything else' else statement at the end also allows us to avoid code dangling outside of the if/elif/else test, so we can remove all of our 'continue's, keeping our code a lot DRYer (avoiding repetition). I know it's kinda late, but hopefully this can help someone later. Happy coding!