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

wafic fahme
wafic fahme
3,309 Points

Whats wrong with this code:

'''python def loopy(items):

for items in loopy: items = input('> ') print (items) if items == "STOP": break

breaks.py
def loopy(items):
    # Code goes here

1 Answer

andren
andren
28,558 Points

There are multiple issues, i'll go through the code line by line

for items in loopy:

Let me first explain how for loops work, a for loop takes a list and assigns each item to a variable within the loop, the first time through the loop the variable will be set equal to the first item in the list, the second time it will be set equal to the second, etc.

In a for loop the word following "for" is the name of the variable you want to hold the individual items in the list you provide, and the word following "in" is the name of the list.

In the above example you provide the name of the list as a variable name and the name of the function as the list name, this is wrong, a more correct example would look like this:

for item in items:
items = input('> ')

And in the above code you are overriding the variable you just defined to hold items with input from the user, the challenge never states that it is is going to provide you with any user input so this line is not necessary.

print (items) 
if items == "STOP":
  break

The challenge states that the code should stop as soon as it comes to an item that contains the string STOP, that means the if statement has to come before the print statement, otherwise the program will print "STOP" before it stops.

The correct code looks like this:

def loopy(items):
    for item in items: 
        if item == "STOP":
            break
        print (item)