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

need help

how to go through this

breaks.py
def loopy(items):
    # Code goes here
Julian Gutierrez
Julian Gutierrez
19,201 Points

Can you paste your attempt too see where you might be going wrong?

def loopy(items):
    for x in y 
    # do something

*Fixed markdown-JG

2 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

Matthew Hill did a pretty good job describing how to go about solving this challenge but I'll expand on it a bit.

You currently have:

def loopy(items):
    for x in y 
    # do something

So the loopy function takes in a single parameter, in this case items. Let's say items is a list that contains four items:

items = ['eggs','milk','STOP','cheese']

In order to iterate over each item in the list we need a for loop, which you have. The structure of a for loop in python is as follows, disregard the quotation marks:

for "variable" in "what you're iterating over":
    do something

To create our for loop we need a variable, which can be anything that follows variable naming conventions, i'll use "item". Next we need the keyword "in", without the quotes, and what we are iterating over in this case "items" again without the quotes. So we now have:

def loopy(items):
    for item in items:

If we just needed to print the items in this list we would be pretty much done, we would just need to print out each item:

def loopy(items):
    for item in items:
        print(item)

However this challenge needs us to check each item in the list to make sure it not the string "STOP". We accomplish this task with an if else statement which is structured like this:

if "condition":
    do something
else:
    do something else

I this example we'll check if the variable item is equal to a string "STOP". If it is we'll break out of the loop but if it isn't we print out the current item:

if item == "STOP":
    break
else:
    print(item)

So now put it all together:

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

The challenge doesn't need you to call the function but if you did you would see that "eggs milk" is printed out because once the function reaches "STOP" it no longer prints "cheese".

Matthew Hill
Matthew Hill
7,799 Points

Try going back to the video and seeing if that gives you any more information. As a hint if you're looping through a list you're probably going to use a for loop:

for x in y:
    # Do something

And if you're going to check a condition you're probably going to use an if else statement in that loop:

if condition:
    # Do this
else:
    # Do this instead

Hope this helps, Matt