Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
What is wrong with my code?
I believe my if statement is not correct.
def loopy(items): for items in items: print(items) if string in items == "STOP": break
A couple of little errors. First you should use a different variable name for the items in a list, or string or whatever you are looping through than the name used for the item itself. For example:
for letter in word: print(letter) for item in lst: print(item) for i in j: print(i)
Second your if statement is outside of your for loop, so the function will loop through whatever you give it printing out each item, and then hen it's finished, check your if statement. just think about the order of operations here. You want to loop through each item in items, and check if it's the string "STOP" before you print it, so the if should be before print and indented the same amount.
Finally You've probably suspected based on what I just said that you can change the if statement, and yep - as well as being before the print, it only needs to check if the current item is "STOP"
So an example: Say I wanted a function that goes through a list of numbers and only prints the even ones, it might look like this:
def evens(items): for number in items: if number % 2 == 0: print(number)
Hope it helps!