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 trialJamal Scott
9,656 PointsI need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in item
I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items. But, if the current thing is the string "STOP", break the loop.
Cant seem to get this one.
Altavious Griffin
Courses Plus Student 2,598 Pointsdef loopy(items): for things in items: print(things)
11 Answers
Nathan Tallack
22,160 PointsTry this.
def loopy(items):
for i in items: # Start loop assigning each item to i
if i == "STOP": # Check to see if i is "STOP"
break # If it is, break out of the loop
else: # Otherwise...
print(i) # Print i
Jeremy Kerrigan
12,002 PointsDon't forget white space matters.
Patric Daniel Pförtner
1,542 PointsHi Jamal,
You are very close to the answer, here is how I did it:
def loopy(items):
# Code goes here
for loopy in items:
print(items)
Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :)
Endrin Tushe
4,591 PointsYep. Definitely helps to think of the items argument being a list rather than a single string.
wafic fahme
3,309 Pointsdef loopy(items):
for things in items:
print (things)
if things == 'STOP':
break
wafic fahme
3,309 PointsWhat is wrong here?
Endrin Tushe
4,591 PointsEven though this is the correct answer. Could someone explain why the loop doesn't actually break when you enter the string STOP in the function. After the function has been defined and run, when you actually type in loopy("STOP") the for loop doesn't actually break, instead it runs through each of the elements in the string and prints them out as:
S T O P
This is the same as what happens when you enter in another word for items such as loopy("apples") you get: a p p l e s
Shouldn't STOP completely break the for loop instead of printing out the elements of STOP?
Charles Ratkie
11,607 Pointsdef loopy(items):
for item in items:
if item == "STOP":
break
else:
print(item)
list = ['apples', 'pears', 'STOP', 'peaches', 'beer']
loopy(list)
So I made a list to give you a better idea of what the loopy function is doing. Then I called loopy with my list as an argument. Because list has 'STOP' in it, loopy will break. If you were to run this in python, you would see it only prints apples, and pears before breaking the loop. Does that help?
Ted Smolinski
1,240 Pointsnot sure if this is correct Python language, but this is what solved the first part of the challenge for me:
def loopy(items): for item in items: if item == "STOP": break else: print(items)
Matthew Costigan
18,319 Pointsdef loopy(items): # Code goes here for item in items: print(items)
Afloarei Andrei
5,163 PointsI'm stuck. I tried this
def loopy(items): for thing in items: if thing == "STOP": break else: print(thing)
and this
def loopy(items): for thing in items: print thing
nothing works.
Afloarei Andrei
5,163 PointsForgot some brackets :p :))
Amekha Sritaboot
795 Pointsdef loopy(items): for loopy in items: print(items)
Altavious Griffin
Courses Plus Student 2,598 Pointsdef loopy(items): [next line and do indent] for item in items: [next line and do indent] print(item)
Give it a try!! Be sure to Reply.
Charles Ratkie
11,607 PointsCharles Ratkie
11,607 PointsIt can be frustrating sometimes when you can't get past the challenge. I did the conditional first and THEN print(thing). Hope that helps!