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 trialAbdullah Jassim
4,551 PointsWhy 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
Alexander Davison
65,469 PointsThe done
function doesn't know that its being called from within a loop, and break
s 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
givens
7,484 PointsI 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