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

Albert Tomasura
Albert Tomasura
11,041 Points

Break Exercise (Objective 2 of 2)

The objective is to create a break in a loop if the current item is "STOP"

This is the code I originally thought up:

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

Apparently it's wrong and I've tried a few other ideas and all of those were wrong as well. I reviewed every lesson covering functions, loops, and if statements and I still have no idea how to resolve this challenge.

breaks.py
def loopy(items):
    for things in items:
        print(items)
    if items == "STOP":
        break

2 Answers

jacksonpranica
jacksonpranica
17,610 Points

Adding on to James South's explanation, not only do you need to change your print(items) to print(things) and your "if items" to "if things", your IF statement needs to be inside the for loop.

Right now the for loop will go through and print everything. You want to make sure that the if statement comes first in the for loop. That way, every item is being checked for if it equals "stop" rather than your code where the for loops prints everything and then checks for the "STOP"

-------ANSWER BELOW-------

def loopy(items):
    for things in items:
        if things == 'STOP':
             break
        print(things)

I can't believe you can't tab on this comment thing...that's annoying

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

items is the list, things are the elements in the list. the if statement needs to be in the for loop, not under it, and it should check if each thing is STOP. right now it is comparing the list to STOP.