12

I am using Python 3.5, and I would like to use the break command inside a function, but I do not know how. I would like to use something like this:

def stopIfZero(a):
    if int(a) == 0:
        break
    else:
        print('Continue')

while True:
    stopIfZero(input('Number: '))

I know that I could just use this code:

while True:
    a = int(input('Number: '))
    if a == 0:
        break
    else:
        print('Continue')

And if you don't care about the print('Continue') part, you can even do this one-liner: while a != 0: a = int(input('Number: '))(as long as a was already assigned to something other than 0) However, I would like to use a function, because other times it could help a lot.

Thanks for any help.

nedla2004
  • 1,115
  • 3
  • 14
  • 29
  • 1
    Your `stopIfZero` function is trying to do more than it should. Try to focus the function on one, and only one, task. – code_dredd Sep 20 '16 at 22:35

4 Answers4

15

Usually, this is done by returning some value that lets you decide whether or not you want to stop the while loop (i.e. whether some condition is true or false):

def stopIfZero(a):
    if int(a) == 0:
        return True
    else:
        print('Continue')
        return False

while True:
    if stopIfZero(input('Number: ')):
        break
JCOC611
  • 19,111
  • 14
  • 69
  • 90
  • I knew that you could do this, but was wondering of you could do it in a different way. I was hoping for a yes, that you could do this, it stinks that you can't. Thank you for your answer, it is what I was asking, even if it is not the yes that wanted. – nedla2004 Sep 20 '16 at 22:01
  • @nedla2004 What applies to `break` also applies to `continue`. – code_dredd Sep 20 '16 at 22:34
8

A function can't break on behalf of its caller. The break has to be syntactically inside the loop.

user2357112
  • 260,549
  • 28
  • 431
  • 505
3

You want to use return, not break.

break is used to stop a loop.

The break statement, like in C, breaks out of the smallest enclosing for or while loop.

return is used to exit the function and return a value. You can also return None.

Andy
  • 49,085
  • 60
  • 166
  • 233
0

The break keyword is meant to be used as in your loop example only and must be inside the loop's scope.

Update your function to return True or False only instead:

def is_zero(value):
    if value == 0:
        return True
    return False

Then in your loop, simply use like this:

while True:
    try:
        value = int(input("Enter a value (0 to exit): "))
    except ValueError:
        # user entered a non-numeric value (e.g. a letter)
        # try asking again immediately instead of crashing :)
        print('Use numbers only, please')
        continue

    if is_zero(value):
        break

    print('Good entry. Continuing...')

Your stopIfZero function is also somewhat overloaded (i.e. has more than just 1 responsibility) because it does more than just check if a value is zero or not. For example, it:

  1. converts the arguments into an int,
  2. prints a message, and
  3. is not re-usable in other parts of your program

Take those reasons into consideration to improve your code.

code_dredd
  • 5,915
  • 1
  • 25
  • 53