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 trialDominick McCleary
3,012 Pointshow do i do a loop inside a function
i tried following the instructions but it i have trouble understanding them how would i make the loop it wants
def loopy(items):
# Code goes here
for item in items:
if item = "STOP":
break
else:
print(item)
5 Answers
Alexander Davison
65,469 PointsYour code is great! It looks fantastic. However, one issue. Two equal signs means equality, and one equal sign means variable assigning :) Your condition says "Python, is assigning a variable true or false?", which of course makes no sense. To clear things up, you must tell Python that you're not assigning a variable, you are checking if your variable is this other value or not. If you want this to work, you must use two equal sign instead of one in your condition. See this:
def loopy(items):
for item in items:
# Over here I used two equal signs! Perfect :)
if item == "STOP":
break
else:
print(item)
Hope this helps! ~Alex
Henning Bang Halvorsen
16,239 PointsYour loop looks fine! It's the if-statement that's wrong :) Remember, when comparing values, in this case 'if item = "STOP"' you need to use double equals!
So try 'if item == "STOP"' instead!
Dominick McCleary
3,012 Pointsthanks
Jiayu Wang
3,314 PointsBut if you run loopy("STOP"), it comes up with:
S T O P
Alexander Davison
65,469 PointsTry entering a list containing "STOP"
Henning Bang Halvorsen
16,239 PointsWhat this code is designed to do is iterate or go through a list of strings and print each one, unless one of them is "STOP", then it will exit. What happens when you pass inn "STOP", is that you do not pass in a list, but a string.
When you loop through a string, it will look at each character individually, and since no single character will ever equal the word "STOP", it will simply print each character.
So as Alexander suggests, pass in a list with only the word "STOP".
Like this:
lst = ["STOP"]
run loopy(lst)
Jiayu Wang
3,314 PointsThanks everyone:)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsI was typing while you were answering :) You typed faster! lol
~alex
Henning Bang Halvorsen
16,239 PointsHenning Bang Halvorsen
16,239 PointsBeat ya to it ! ;)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsI gave ya an up-vote :)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsLol this is what took me so long answering: I made a great answer but I refreshed the page by accident... slaps forehead next time i'll use a text editor to answer
Dominick McCleary
3,012 PointsDominick McCleary
3,012 Pointsyes it does thank you