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

Need Help Breaking

So I need help. My code as you can see below has part where its supposed to break. but I keep getting an error. I’ve tried similar problems on python shell and it works properly. why won’t this work???

breaks.py
def loopy(items):
    for item in items:
        print(item)
        if items == "STOP":
            break
Antonio De Rose
Antonio De Rose
20,885 Points
def loopy(items):
    for item in items:
        print(item)#if you print it here, then you are printing 'STOP,' provided the first string from the list is 'STOP'
        if items == "STOP":#item is the string, and items is the list, are you doing it right, 
#the above line to come after def, then to follow break, then print
            break

Hmmm.... Didn’t work, here’s my new code:

def loopy(items):
    for item in items:
        if item == "STOP":
            break
        print(items)
Antonio De Rose
Antonio De Rose
20,885 Points

Good try, I do not see an issue with your code, if there is, I think, the indenting issue, could you please paste your code with a proper markdown, just like your first time.

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Albrino Fernando

So you almost are there.

Kenneth doesn't want you to print the whole list of items, only each item. And then it asks you to conditionally check that item, if it equals "STOP" you want to break instead of print.

So

def loopy(items):
    # items is a list of things

    for item in items: # looping through each item
        if item == "STOP": # conditionally check if a single item == "STOP"
            break # if condition is True, we break
        print(items) # if we reached this spot, we never break from loop. print the item.

I gave you lots of hints in those comments. See if you can find your error. You were close. Let me know if that helps.

I Tried Something New But I’m Still Getting An Error, New Code:

def loopy(items):
    for item in items:
        if item !== "STOP":
            print(item)
        else:
            break
Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hey Albrino Fernando

So it looks like you are using an operator that you will see in other languages, but doesnt exist in Python

Python does not have the "strict" operator: !== you are trying to use as some other languages have.