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

Sadra Aghazadeh
Sadra Aghazadeh
785 Points

breaks in python

Hello,

I am not sure what I am doing wrong with my code. The question wants me to break the for loop if an item in the list is equal to "STOP".

I am not sure what I am doing wrong with my code.

breaks.py
def loopy(items):
    for item in items:
        print(item)
        if item = 'STOP':
            break
nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Can you copy paste the entire question so I can read it first hand :)

Sadra Aghazadeh
Sadra Aghazadeh
785 Points

Challenge Task 1 of 2

I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items. Reminder: Check your syntax and indenting!

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!

2 Answers

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Perhaps it wants you to break out of the loop before you print "STOP" (meaning "STOP" never gets printed).

def loopy(items):
   for item in items:
       if item == 'STOP':
           break
       else:
           print(item)
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Correct. The Challenge asks for the items to be printed ... but only until the loop hits "stop" So, the logic needs to check the word first... then print it. :smile: :thumbsup:

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Hello Sandra,

In your 4th line of code you are intending to test for equality. The syntax for that is ==. a single = refers to assignment!

I hope this helps :)

Sadra Aghazadeh
Sadra Aghazadeh
785 Points

HI Nicole,

Thanks for your reply. However, when I do test for equality, it says "Didn't find the right items being printed".

Here is the question that I must answer: "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!"