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

how 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

breaks.py
def loopy(items):
    # Code goes here
    for item in items:
        if item = "STOP":
            break
        else:
            print(item)

5 Answers

Your 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 :smile:

I was typing while you were answering :) You typed faster! lol

:smile: ~alex

I gave ya an up-vote :)

Lol 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

yes it does thank you

Henning Bang Halvorsen
Henning Bang Halvorsen
16,239 Points

Your 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!

thanks

Jiayu Wang
Jiayu Wang
3,314 Points

But if you run loopy("STOP"), it comes up with:

S T O P

Try entering a list containing "STOP"

Henning Bang Halvorsen
Henning Bang Halvorsen
16,239 Points

What 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
Jiayu Wang
3,314 Points

Thanks everyone:)