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

why is my code not working

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

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

1 Answer

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Franco,

To begin with, do not forget to end each statement with a semicolon( ; ). Change your break and print statement accordingly.

To continue, in the for loop the variable which stores the value of each element in items has the same name as the array. This is certainly something to change. You can see why if you dive a bit deeper into what this is causing.

Also, in the if statement you want to check if the element equals "STOP", not to assign a value to the element.

Finally, you need to indent your print statement correctly so it lies inside the for loop. Each element of the array should be printed no?

Applying these changes will lead to something like this,

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

Note: The challenge does not require to check whether one of the elements equals "STOP". Actually you should not include this check as "STOP" is one of the elements in the array and needs to be printed.

Hope that helped!

Ryan S
Ryan S
27,276 Points

Hi Ioannis,

You have provided the correct solution with some good tips but I thought I'd just jump in here to clear up a couple things.

First, Python does not require semi-colons to terminate statements. Although it will still pass in most circumstances, it is not really considered good practice and is usually frowned upon. Plus it is one extra keystroke you don't need. Instead, Python uses white space and new lines to interpret statement termination.

In Python, semi-colons are considered separators. So you could use them to write multiple statements on one line, but again that is not really the common style of Python. This is actually the reason why semi-colons won't usually cause any problems at the end of lines. They are essentially separating the statement with white space, and it is the white space that terminates. So they are basically redundant in this type of usage.

Regular colons, however, must be used on conditional statements and function/class definitions, etc. So Jesus is correct on his punctuation.

And second, Task 2 in this challenge does ask to insert a "check if 'STOP'" type command. It's just building on the simple loop created in Task 1.