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 trialSimon Perez
Courses Plus Student 915 Pointswhy 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?
sylwia
6,965 PointsWe 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.
annecalija
9,031 Pointsannecalija
9,031 PointsIf 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.
Hope this helps.