Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Shopping List App Break

Challenge 2 of 2 Shopping List App Error

def loopy(items): # Code goes here current_item = input("> ") for item in items: if current_item == "STOP": break print(item)

breaks.py
def loopy(items):
    # Code goes here
    current_item = input("> ")
    for item in items:
        if current_item == "STOP":
            break
    print(item)        

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,704 Points

Hey there,

You seemed to go a bit off track for the second part of the Challenge. You had it all going okay for the first one... Why did you change the name of variables and add a current_item variable. None of this was asked for in the second task. You do have all the 'elements' needed for the second task, but...

Your code from the first task:

def loopy(items):
    # Code goes here
    for item in items:    
        print(item)

Just needs 2 lines added inside of the for loop.

So, to fix your code up... first you need to delete the current_item = input("> "). This was not asked for by the challenge's instructions. The instructions are always very specific and need to be followed exactly, so I'm not sure why this was added?

Next, you need to change the variable names back to what you had for the first task. There was no need to change them, so I'm a bit confused as to why you did? You're looping over items and storing the value of each iteration in the temporary variable item. This is the only variable that you need to (and can) check against for the word "STOP" in the if statement.

Finally, you need to put the print method back inside of the for loop. Python is very indent dependent and by removing the indent, you moved it outside of the loop, so now the iterations through the loop are not being printed out any longer.

So, once all that is done, you end up with

def loopy(items):
    # Code goes here
    for item in items:
        if item == "STOP":
            break
        print(item)

I hope this helps. :)

Keep Coding! :dizzy: