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 trialSteve Weiland
2,644 PointsNeed help with adding a break to a loop
I'm not sure why my code isn't working properly. Do I maybe have it in the wrong area? Do I need to create a new variable before I'm able to break the loop? Please help!
def loopy(items):
for item in items:
print(items)
if item == 'STOP':
break
# Code goes here
1 Answer
Steve Hunter
57,712 PointsHi Steve,
You need to indent the break
by a further four characters. And also the if
should be indented a further 4 spaces too as it is inside the loop. So the break
command will be indented 12 spaces in total.
Also, it should come before the print
command as the loop should break prior to STOP
being printed.
Steve.
Steve Weiland
2,644 PointsSteve Weiland
2,644 PointsThank you very much Steve for the help! It looks like I had the right idea just needed to know the placement of the code and to add my spaces.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsAbsolutely - your code was correct but lacking some indenting.
One things that surprised me was that your
print
command passed the code challenge. You are looping overitems
, and at each pass, you are storing each element ofitems
in the variableitem
. You could equally call that variablei
or whatever you choose.Your code then prints the iterable,
items
, rather than the individual element,item
. I think that's a technical issue with this challenge but I think it is worth noting. So, just as you testeditem
to be equal to'STOP'
, similarly, the output should useitem
, rather thanitems
.I hope that makes sense!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsInterestingly, and incorrectly, the first part of this challenge can be passed with:
I've notified support of this apparent bug.