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

Clayton McDaniels
Clayton McDaniels
5,788 Points

Why is the code below not working for this challenge?

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

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

2 Answers

Hi Clayton,

If the user enters STOP, don't print but just break. So, put your if before the print line.

Also, you want to see if thing equals STOP, not items.

Steve.

Clayton McDaniels
Clayton McDaniels
5,788 Points

Copying the if before the print line worked! All else is fine! Thank you!

I found that just moving the if wasn't sufficient. This doesn't work:

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

Whereas changing if items == "STOP": to if thing == "STOP": meant that the code is comparing the for loop variable which holds each element of items in turn, which is thing. This code worked:

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

Steve.

Clayton McDaniels
Clayton McDaniels
5,788 Points

Steve, your followup reply makes more sense and is more logical. I thought that should work too, however the challenge accepted just moving the if statement.

You got lucky, I reckon. I tried it with just moving the statement and it failed. Still, it doesn't matter! Enjoy the rest of the course!