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 trialbrucebrown
1,059 PointsCan't figure out how to use break properly on coding challenge in python
def loopy(items): for word in items: print(word) if items == "STOP": break
I've tried replacing word with items and moving the if statement to the same indentation as for.
1 Answer
Brandon Oakes
Python Web Development Techdegree Student 11,501 PointsSo it looks like your function is taking one argument(items), which I am assuming is a list. Then you have a for loop that is taking each element(word) in the list and is printing that element(word). Why don't you incorporate a if/else statement. You just have a if statement.
for word in items: if word == "STOP": break else: print(word)
the if/else statements will allow you to tell python "hey do this IF this is true, ELSE always do something else". Also in your code, you are looping through a list to look at WORD but you are telling python to break if ITEMS== "STOP".
brucebrown
1,059 Pointsbrucebrown
1,059 PointsAlso how do I copy and paste code every time I try it just get condensed into one line