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 Introducing Lists Build an Application Display the List

Why 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
Louise St. Germain
19,424 Points

Hi!

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.

Ah I see.. Thank you so much!! I get it now

Hello Louis,

It seems I'm still getting the "SHOW" in my list even when I add the continue, see below:

shopping_list = []


def show_help():
    print("What should we pick up at the store?")
    print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to see current list.
""")


def add_to_list(item):
    shopping_list.append(item)
    print("Added! List has {} items.".format(len(shopping_list)))


def show_list():
    print("Here's your current list:")
    for item in shopping_list:
        print(item)


show_help()
while True:
    new_item = input("> ")
        add_to_list(new_item)
    if new_item == 'DONE':
        break
    elif new_item == 'HELP':
        show_help()
        continue
    elif new_item == 'SHOW':
        show_list()

        continue

show_list()