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 trialdc16
3,439 PointsWhy new_item == 'SHOW' which isn't an assignment operator without "continue" end up in shopping list?
Can someone please help? I'd want to know why a comparison operator here ended up acting like an assignment operator without "continue" and the shopping_list got appended with SHOW
1 Answer
Louise St. Germain
19,424 PointsHi!
Good question! In fact the comparison operator does not act as an assignment operator without the continue. It is still a comparison. What is happening is this. If you forget "continue", the code looks like this:
# etc... some code before this
while True:
new_item = input("> ")
if new_item == "DONE":
break
elif new_item == "HELP":
show_help()
continue
elif new_item == "SHOW":
show_list()
add_to_list(new_item)
# etc... more code after this
When running the program, if you type "SHOW", that is the value of new_item. Python is looping through the while loop. It gets a successful match on the line elif new item == "SHOW": so it starts running the code inside there, which is the show_list() command. So far, so good.
But without a continue command, which forces it back to the top of the while loop, it just keeps going! So it ends up running that add_to_list(new_item) command below it, which is not what you intended. new_item contains "SHOW", so it does add_to_list("SHOW"), and that's how "SHOW" would end up in the shopping list.
To avoid this, you would need to have a continue after the show_list() item, like this:
# etc... some code before this
while True:
new_item = input("> ")
if new_item == "DONE":
break
elif new_item == "HELP":
show_help()
continue
elif new_item == "SHOW":
show_list()
# Add continue here... so that it then goes up to the top of the while loop again.
continue
add_to_list(new_item)
# etc... more code after this
That way it won't run the add_to_list command if new_item is "SHOW".
I hope this helps! Let me know if you still have any questions.
dc16
3,439 Pointsdc16
3,439 PointsAh I see.. Thank you so much!! I get it now
Jamar Slade
2,480 PointsJamar Slade
2,480 PointsHello Louis,
It seems I'm still getting the "SHOW" in my list even when I add the continue, see below: