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