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
Philip Matunda
Courses Plus Student 510 Pointsbreak function
def loopy(items):
#code goes here
for item in items:
print(item)
if item =="STOP":
break
print(item)
list=["apple", "orange", "STOP", "pears"]
loopy(list)
I've been trying to run the code but it's yielding a bummer
def loopy(items):
#code goes here
for item in items:
print(item)
if item =="STOP":
break
print(item)
list=["apple", "orange", "STOP", "pears"]
loopy(list)
1 Answer
Steve Hunter
57,712 PointsHi Philip,
You have unindented your if statement so it is outside the forloop. Indent the if statement and the break by 4 spaces.
Also, only output the print after the if statement, but inside the loop. i.e. if item is 'STOP' nothing gets printed.
Steve.
Philip Matunda
Courses Plus Student 510 PointsPhilip Matunda
Courses Plus Student 510 Pointsis it something like this
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNearly. Remove the first
printstatement - you want that after theifstatement, but not within it. So, unindent the secondprintstatement by 4 spaces - it needs to be inside theforloop but outside theifstatement. Also, add brackets arounditemin yourprintstatement.Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI added some comments in your code.
Philip Matunda
Courses Plus Student 510 PointsPhilip Matunda
Courses Plus Student 510 PointsThanks bro, it worked.
Steve Hunter
57,712 PointsSteve Hunter
57,712 Points