Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Shopping List App Second Shopping List App

Simon Perez
PLUS
Simon Perez
Courses Plus Student 915 Points

why didn't we use the else at the end of the loop?

at the end of the loop, we didn't use the else, we directly wrote the functionality, why is that?

If you're referring to the last line of code inside the while which is add_to_list(new_item), the reason is that we used break and continue in the if and elifs. That means, an iteration will never reach that last line of code whenever it meets the conditions DONE, SHOW and HELP, so else statement is no longer needed.

All user input which is not DONE, nor SHOW and nor HELP is assumed to be an item that will be added to the list.

Whether you put else there or not, the program will still work. It is just a matter of which lines of code are needed and not. On the other way around, if you use else, you may want to remove continue.

while True:
    new_item = input("> ")
  # be able to quit the app
    if new_item == 'DONE':
        show_list()
        break
    elif new_item == 'HELP':
        show_help()
    elif new_item == 'SHOW':
        show_list()
    else:
        add_to_list(new_item)

Hope this helps.

We do not add this because it is implicit. If you have an if/else (or if/elif/else) block it means that SOMETHING WILL DEFINITELY catch your code. Unless of course there is an error, but let's assume there is not.

Numbers are easy to think about it:

num = 5

if num > 5:
    print("this is more than 5")
elif num < 5:
    print("this is less than 5")

Your number is 5 which means it was not caught by either of the conditions. IT MUST BE a 5! (again, no errors allowed, and we only care about numbers).

Now you can simply compare this version:

num = 5

if num > 5:
    print("this is more than 5")
elif num < 5:
    print("this is less than 5")
else:
    print("This is a 5!!!")

to this one:

num = 5

if num > 5:
    print("this is more than 5")
elif num < 5:
    print("this is less than 5")
print("This is a 5!!!")

Many people think it is self-explanatory and like to remove what is seen as an unnecessary code. At the beginning, it is a little strange, but you get to appreciate it.