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 trial

Python Python Basics (2015) Shopping List App Break

What is wrong with my code?

I believe my if statement is not correct.

breaks.py
def loopy(items):
    for items in items:
        print(items)
    if string in items == "STOP":
        break

1 Answer

Hi there!

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!