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 Shopping List Introduction

Robert Baucus
Robert Baucus
3,400 Points

(Python3.6) ShoppingList program gets 'break' error

I got the program to run using a while loop but I am having a problem getting it to run using a function. I have gone through it a couple times and think it looks ok, but I keep getting a " syntaxError: break outside loop" , this is what I have:

print("Let's make a shopping list, breh!")
print("Add new items one at a time, type, 'DONE' to finish")

shopping_list = []

def shopping():
    new_item = input("> ")
    if new_item.upper() == 'DONE':
        break
    shopping_list.append(new_item) 
    return shopping_list

print("Here's your shoping list, breh!")
print(shopping_list)

any ideas?

1 Answer

akhter ali
akhter ali
15,778 Points

break is not meant for if/else condition, break is meant to break out of for/while loops. Your break statement is invalid.

Take the example from the link below, break is breaking out of the inner for loop and only runs if the condition is True/False

https://docs.python.org/3.6/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops