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 trialLuke Bennett
10,265 Pointsfor loop sending me loopy - What is wrong with this code?
I don't understand where I have gone wrong, I've gone over and over it, I'm sure it is something silly, but the error code is extremely vague as well.
def loopy(items): for item in items: if item == "STOP": break print(item)
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
2 Answers
tjgrist
6,553 PointsYou're so close! It wants you to add an else statement:
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
else:
print(item)
tjgrist
6,553 PointsWell, you're initial code would work without an else statement, but you would have to change the indentation. Python takes indentation very seriously. So, you had:
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
The problem above is that the print(item) call is outside of the for loop. In order to print every item, you have to call print(item) from within the for loop. Like this (notice the indentation of the print(item)):
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
The above is functionally the same as having an else: statement that calls print(item).
So, I believe most of you're issues with the code are just indentation errors. Does that answer anything?
Luke Bennett
10,265 PointsIf it was indentation error wouldn't there be an error message though? There is no error message in the console when I request the file.
Also, thank you for being so patient!
Luke Bennett
10,265 PointsI understand why it wasn't working in workspaces now, sorry about that. Something silly, I forgot to call it with loopy(items). Thank you for all your help, you helped me find it anywhoo
tjgrist
6,553 PointsYes, not an indentation error, but rather a scope issue. You want the print(item) to be in the scope of the for loop, but originally, you had the call to print(item) outside the for loop, so it would either error becuase item is undefined in that scope or it would only print the "stop" item, I don't remember exactly which would happen. Glad you got it working!
haha, yes, many of us forget to call the functions! You will remember next time.
tjgrist
6,553 PointsYou are very new to Python it looks like, so good job! have fun
Luke Bennett
10,265 PointsLuke Bennett
10,265 PointsOHHHH okay, so it would print without break? but obviously with the 'break' there needs to be another question?
Luke Bennett
10,265 PointsLuke Bennett
10,265 PointsIn fact, I do not understand haha. Why does this work: names = ['Kenneth', 'Amy', 'Andrew', 'QUIT', 'Lacey'] for name in names: if name == 'QUIT': break print(name)
but the loopy function does not without else.
Luke Bennett
10,265 PointsLuke Bennett
10,265 PointsThis also won't work in my workspaces either:
items = ['Bob', 'Emily', 'STOP', 'X']
def loopy(items): for item in items: if item == "STOP": break else: print(item)
It passed the test, but will not work in workspaces
tjgrist
6,553 Pointstjgrist
6,553 PointsWithout your examples in code, I can't see indentation, so it's hard to see exactly how it's working. remember to use the code styling in your comments! :)