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 trialAndrey Klimushin
3,614 PointsPls help me optimize the code
the code is to long to run
def loopy(items):
# Code goes here
loopy=[]
while True:
if items=='STOP':
break
for items in loopy:
print(items)
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsThe issue is if your code is run with a list items that does not have a "STOP" among them, the while
loop will never terminate. The while
loop isn't necessary. A for
loop can walk down the items list printing until a "STOP" is seen. If not seen, the function completes naturally.
def loopy(items):
# check each item in the items list
for item in items:
# check if this item is STOP
if item == 'STOP':
# end the for loop
break
# otherwise print
print(item)
Andrey Klimushin
3,614 PointsThat`s make sense now. Thanks again Chris!