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 trialAhmed Al-Alawi
Front End Web Development Techdegree Student 17,165 PointsWhere is the "else" keyword in the if else statement in the video? how his code is still working with no else 'keyword'
but as I omit the else in my code down below, the "Added....." is not printed and also the list items after DONE are not being printed
The code without the "else" keyword
shopping_list = list()
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
continue
print("Here's your list:")
for item in shopping_list:
print(item)
the result in the console
Last login: Sat Sep 5 16:28:32 UTC 2015 on pts/0
treehouse:~/workspace$ python shopping_list.py
What should we pick up at the store?
Enter 'DONE' to stop adding items.
Apple
Orange
Banan
DONE
Here's your list:
treehouse:~/workspace$
[edit formatting --cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsAfter reformatting your post, the else
is required in your code due to the indentation.
if new_item == 'DONE': #<-- Start code block if 'DONE'
break #<-- immediately break from code block. Start next while look iteration
# The following code is never executed since it's after the `break` and
# it's inside the `if` code block
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
continue
Instead, unindent the three lines or add an else
if new_item == 'DONE': #<-- Start code block if 'DONE'
break
# unindented next three lines to run outside of `if` code block
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
continue
if new_item == 'DONE': #<-- Start code block if 'DONE'
break
else #<-- Added else to execute the remaining statements
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
continue
Finally, the continue
statement is not needed if it's the last statement. A "continue" is implied.
C H
6,587 PointsYou can have 'if' statements on their own without any else statement following. It would just mean that if the 'if' condition is not met, it skips it and keeps going. I guess the entire rest of the code could be put in an else statement, but it is unnecessary.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYou don't need to use the 'else' keyword always, you could just do 'if'. It's optional.