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

Philip Matunda
PLUS
Philip Matunda
Courses Plus Student 510 Points

break function

def loopy(items):


    #code goes here
    for item in items:
        print(item)
    if item =="STOP":
        break
    print(item) 

list=["apple", "orange", "STOP", "pears"]

loopy(list)

I've been trying to run the code but it's yielding a bummer

breaks.py
def loopy(items):


    #code goes here
    for item in items:
        print(item)
    if item =="STOP":
        break
    print(item) 

list=["apple", "orange", "STOP", "pears"]

loopy(list)

1 Answer

Hi Philip,

You have unindented your if statement so it is outside the forloop. Indent the if statement and the break by 4 spaces.

Also, only output the print after the if statement, but inside the loop. i.e. if item is 'STOP' nothing gets printed.

Steve.

Philip Matunda
Philip Matunda
Courses Plus Student 510 Points

is it something like this

def loopy(items):
    # Code goes here
    for item in items:
        print(item) # remove this line
        if item == 'STOP':
            break
            print (item) # unindent this by 4

Nearly. Remove the first print statement - you want that after the if statement, but not within it. So, unindent the second print statement by 4 spaces - it needs to be inside the for loop but outside the if statement. Also, add brackets around item in your print statement.

Steve.

I added some comments in your code.

:+1: :+1: