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 trialValerio Versace
11,319 PointsShopping list While loop
Kenneth does it like this:
show_help()
while True:
new_item = input("> ")
if new_item == "DONE":
break
elif new_item == "HELP":
show_help()
continue
elif new_item == "SHOW":
print_list()
continue
add_to_list(new_item)
print_list()
Why all those "continue"?
Wouldn't it be better to do it like this?
show_help()
while True:
new_item = input("> ")
if new_item == "DONE":
break
elif new_item == "HELP":
show_help()
elif new_item == "SHOW":
print_list()
else:
add_to_list(new_item)
print_list()
2 Answers
Evan Demaris
64,262 PointsHi Valerio,
I think the reason why Kenneth worked his code that way is that he's trying to teach how continue
works. Your code would be more efficient, though.
Colby Work
20,422 Pointswithout the continue, the words HELP and SHOW get added to the list. he briefly explains it at 3:40.
Evan Demaris
64,262 PointsActually, that isn't the case with Valerio's code.
Valerio moved the add_to_list(new_item)
into an else
block, so it won't be executed unless DONE
, HELP
and SHOW
are all != to new_item
.
Colby Work
20,422 Pointsah... i see that
Valerio Versace
11,319 PointsValerio Versace
11,319 PointsCool, didn't think about that! Makes sense.