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 trialmaike willuweit
552 Pointsmy break command is not working for a list
I am trying to add a break into the easy script, but I'm failing to see what I did wrong...
def loopy(items):
# Code goes here
for item in items:
print(item)
if item == "STOP":
break
2 Answers
Alexander Davison
65,469 PointsTwo problems:
- The
if
statement to Python is outside of thefor
loop, you should move in the condition by adding an extra 4 spaces to the last two lines of code. - This is very confusing, but the
if
condition has to be before theprint()
line of code. If we pretend to run our code, the result will be (we are calling the function with the argument [1, 2, 3, 4, 5, "STOP"]):
1
2
3
4
5
"STOP"
However, the code challenge isn't expecting you to print "STOP" at the end.
To fix that, you must move the if
condition above the print statement.
Answer:
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
Good luck! ~Alex
maike willuweit
552 Pointsthanks for the nice explanation! It works :-)!
Alexander Davison
65,469 PointsNo problem :)