ned*_*004 6 python function break python-3.x
我正在使用Python 3.5,我想break在函数内部使用命令,但我不知道如何.我想用这样的东西:
def stopIfZero(a):
if int(a) == 0:
break
else:
print('Continue')
while True:
stopIfZero(input('Number: '))
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用这段代码:
while True:
a = int(input('Number: '))
if a == 0:
break
else:
print('Continue')
Run Code Online (Sandbox Code Playgroud)
如果你不关心这个print('Continue')部分,你甚至可以做一个单行:(
while a != 0: a = int(input('Number: '))只要一个已经分配给0以外的东西)但是,我想使用一个函数,因为其他时候它可以帮助很多.
谢谢你的帮助.
通常,这是通过返回一些值来完成的,该值允许您决定是否要停止while循环(即某些条件是true还是false):
def stopIfZero(a):
if int(a) == 0:
return True
else:
print('Continue')
return False
while True:
if stopIfZero(input('Number: ')):
break
Run Code Online (Sandbox Code Playgroud)