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

victor E
victor E
19,145 Points

not sure how I'm getting this wrong

not sure what I'm typing wrong

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

    if items == "STOP" :
        break

3 Answers

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

think about your for loop: for words in items. what does words represent, and what is items? which are you printing? which are you checking is equal to STOP? what does the challenge ask you to do? does it want you to print STOP then break, or break if you hit STOP, before you print STOP?

Hi Victor,

In this challenge, we need to create a for loop that will go through each item in the items list and print them to the screen. Part b just wants us to add an if statement to look for the string "STOP" in the items list, and if it is found, to exit/stop the for loop.

First thing to note, the keyword "break" is used to stop/exit a loop. So we're saying that when the string "STOP" is found in items, we want the for loop to exit. So that means that the "if" statement needs to be inside the for loop code, instead of outside it.

The next thing to note is how if statements work. Best practice is that if you are using an if statement to look for something specific, you should also have an else statement to handle what happens when the code inside the if doesn't find what it's looking for. In this case, your if statement is looking for the string "STOP" and if found, it stops the loop, if "STOP" isn't found, then what?

So you could do something like this: for item in items: if item == "STOP": break else: print(item)

David Moriarty I did that and that did not work

victor E
victor E
19,145 Points

This is what I have and it is still not working. I think the mistake is on the "else" statement.

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