Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

I'm utilizing Python 3.5, and I might want to utilize the break command inside a function, yet I don't ability. I might want to utilize something like this: 

def stopIfZero(a):

    if int(a) == 0:

        break

    else:

        print('Continue')

while True:

    stopIfZero(input('Number: '))

I know, that I can also use the below code:

while True:

    a = int(input('Number: '))

    if a == 0:

        break

    else:

        print('Continue')

What's more, in the event that you couldn't care less about the print('Continue') part, you can even do this in one line: while a != 0: a = int(input('Number: '))(as long as a was at that point alloted to some different option from 0) However, I might want to utilize a function, on the grounds that different occasions it could help a great deal.

Thank you

1 Answer

0 votes
by (26.4k points)

Typically, this is finished by returning some worth that allows you to choose whether or not you need to stop the while loop(for example regardless of 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

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 22, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...