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

This is foolish My code is correct why am i not getting it right this is dumb.

Challenge Task 2 of 2

Oops, I forgot that I need to break out of the loop when the current item is the string "STOP". Help me add that code!

i did all that but nothing happens

breaks.py
def loopy(items):
    for item in items:
        print(item)

        if item == "STOP":
            break


loopy("STOP")

8 Answers

Hi William,

You're so close, man!. But, you have a logic error: calling print() before your if statement defeats the purpose of the if statement. Let me know if that helps.

Also, there's no need to actually call loopy() at the end; the Challenge never asked you to do that.

thank you very much man

i was so fed up because i read the thing and wrote everything but didn't realize that logic.

that means i could just use a try and catch then instead of an if statement that would be pretty genius based on what i learnt so far.

Eddie Russell
Eddie Russell
617 Points

Hello Mikis,

Thank you. I was having the same issue. I don't think I would have ever figured that out on my own. Did I miss something in a previous lesson? Also, is there a way to go back and look at the code challeges that I've completed? I ask because sometimes while I'm woking on a challenge I try a bunch of different things and when one of them works the challenge ends and I don't always remeber exactly what I did. I would like to be able to study what I did to finish the challenge.

Glad it helped you out, Eddie! And I know what you mean about that uncertain feeling you get when one of your trial-and-error solutions ends up working but you have no idea why. Unfortunately, Treehouse doesn't offer any functionality to students that would allow them to take notes β€”Β or save a challenge question. I'll send a feature request to the appropriate people about it for you, if you want.

But, to be honest, I'm not sure it'd do you much good. Programming is a skill-set like any other and you can only really begin to grok the underlying concepts once you start building stuff. Imagine a skill like playing golf. You could read all the Golf for Dummies books you want, you could watch all the professionals do their thing, but until you actually get out there and try to hit that tiny-ass white ball with the skinniest metal stick ever made, you don't really know anything about Golf. Programming is the same way. Until you actually build a fully-functioning program that an end-user can actually use, you won't really understand what building software is all about. I hope that helps. Good luck ;)

Mikis thanks this helps!

Jason Prince
Jason Prince
5,113 Points
def loopy(items):  # Gives a name to the function and provides the list (items).
    for item in items:  # Each item in the list will be assigned to the item variable.
        if item is "STOP":  # If an item in the items list is "STOP"...
            break  # stop the loop.
        else:  # If an item in the items list is anything but "STOP"...
             print(item)  # print the item.
Kanthad Yangsattha
Kanthad Yangsattha
946 Points

life saving, my friend. I didn't know that I have to use "else".

Max Krill
Max Krill
645 Points

nice one! thanks

Rosario Gomez
Rosario Gomez
10,678 Points

Hi Mikis, thank you for your answer!

It does makes sense, unfortunately I still can get this challenge right =(

I changed my code into this:

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

But I'm still getting an error.

Rosario Gomez
Rosario Gomez
10,678 Points

Here's the code:

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

still with ERROR

pls someone can pls help me?? i can't find the correct answer, this is my code: def loopy(items): for item in items: if item == "STOP": break for item in items: print(item)

def loopy(items):

Code goes here

for item in items: if item =="STOP": break else : print(item) this is the answer but place indents properly if indent ="properly"; code runs else: error

erm.. why is it

for item in items:

when the part before was

for loopy in items:

Rosario Gomez
Rosario Gomez
10,678 Points

Hi everyone! I'm stuck in this same code challange and I don't understand what I'm doing wrong, here's the code I've submitted:

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

Could anyone help me, please?

Thanks in advance =)

Hey Rosario,

Your indentation is all over the place β€” but that may be because of the copy-and-paste. Besides that, you have a few logic errors. The Challenge asked you to loop through each item then β€” while still in the loop β€” check if that item is equal to the string "STOP." Whenever a given item is equal to the string "STOP," you want to break out of the loop; otherwise, you want to print the item. Let me know if that makes sense.