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 trialSadra Aghazadeh
785 Pointsbreaks 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.
def loopy(items):
for item in items:
print(item)
if item = 'STOP':
break
Sadra Aghazadeh
785 PointsChallenge 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
Courses Plus Student 5,328 PointsPerhaps 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
Treehouse Moderator 145,860 PointsCorrect. 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.
nicole lumpkin
Courses Plus Student 5,328 PointsHello 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
785 PointsHI 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!"
nicole lumpkin
Courses Plus Student 5,328 Pointsnicole lumpkin
Courses Plus Student 5,328 PointsCan you copy paste the entire question so I can read it first hand :)