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 Python Basics (2015) Shopping List App Break

for loop sending me loopy - What is wrong with this code?

I don't understand where I have gone wrong, I've gone over and over it, I'm sure it is something silly, but the error code is extremely vague as well.

def loopy(items): for item in items: if item == "STOP": break print(item)

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

2 Answers

You're so close! It wants you to add an else statement:

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

OHHHH okay, so it would print without break? but obviously with the 'break' there needs to be another question?

In fact, I do not understand haha. Why does this work: names = ['Kenneth', 'Amy', 'Andrew', 'QUIT', 'Lacey'] for name in names: if name == 'QUIT': break print(name)

but the loopy function does not without else.

This also won't work in my workspaces either:

items = ['Bob', 'Emily', 'STOP', 'X']

def loopy(items): for item in items: if item == "STOP": break else: print(item)

It passed the test, but will not work in workspaces

Without your examples in code, I can't see indentation, so it's hard to see exactly how it's working. remember to use the code styling in your comments! :)

Well, you're initial code would work without an else statement, but you would have to change the indentation. Python takes indentation very seriously. So, you had:

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

The problem above is that the print(item) call is outside of the for loop. In order to print every item, you have to call print(item) from within the for loop. Like this (notice the indentation of the print(item)):

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

The above is functionally the same as having an else: statement that calls print(item).

So, I believe most of you're issues with the code are just indentation errors. Does that answer anything?

If it was indentation error wouldn't there be an error message though? There is no error message in the console when I request the file.

Also, thank you for being so patient!

I understand why it wasn't working in workspaces now, sorry about that. Something silly, I forgot to call it with loopy(items). Thank you for all your help, you helped me find it anywhoo

Yes, not an indentation error, but rather a scope issue. You want the print(item) to be in the scope of the for loop, but originally, you had the call to print(item) outside the for loop, so it would either error becuase item is undefined in that scope or it would only print the "stop" item, I don't remember exactly which would happen. Glad you got it working!

haha, yes, many of us forget to call the functions! You will remember next time.

You are very new to Python it looks like, so good job! have fun