Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Shopping List App Break

Clayton McDaniels
Clayton McDaniels
5,776 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,776 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,776 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!