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 Introducing Lists Build an Application The Application

Abdullah Jassim
Abdullah Jassim
4,551 Points

Why cant I use the done break as a function? Thanks.

def done():
    if new_item == "done".lower():
        break

while True:
    new_item = input("> ")
    done()

2 Answers

The done function doesn't know that its being called from within a loop, and breaks aren't allowed outside of loops, so Python throws an error.

Try simply not making a function instead:

while True:
    new_item = input("> ")
    if new_item == "done".lower():
        break

I believe Alexander has the right answer. I checked that breaks aren't allowed outside loops and found that they are indeed not allowed. This is my test code.

test.py code:

def done(new_item):
    if new_item.lower() == "done":
        break

while True:
    new_item = input("> ")
    done(new_item)

Console output:

treehouse:~/workspace$ python test.py                                                            
  File "test.py", line 3                                                                         
    break                                                                                        
    ^                                                                                            
SyntaxError: 'break' outside loop