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 trialanhedonicblonde
3,133 Pointsbreaks.py question - Python basics
I'm stuck on this. I am missing a major component of the code I need but I cannot figure out what it is. Can someone point out what I am doing wrong here? Thanks in advance!
def loopy(items):
for item in items:
while True:
print(item)
if item == "STOP"
break
# Code goes here
1 Answer
Gianpaul Rachiele
4,844 Pointsdef loopy(items):
for item in items:
if item == "STOP":
break
else:
print(item)
This should work. The while loop isn't necessary. If you just have a for loop that you want to break you should have the conditions that are going to break it first and anything else that comes after it execute. Hope this helps.
anhedonicblonde
3,133 Pointsanhedonicblonde
3,133 Pointsthank you so much! You have helped me tremendously! :)