• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在函数内部使用Python断

用户头像
it1352
帮助12

问题说明

我使用的是Python3.5,我想在函数中使用break命令,但我不知道如何使用。 我想使用这样的东西:

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

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

我知道我可以只使用以下代码:

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

如果您不关心print('Continue')部分,您甚至可以执行以下一行操作: while a != 0: a = int(input('Number: '))(只要已将分配给非0的对象) 但是,我想使用函数,因为其他时候它可能会有很大帮助。

谢谢您的帮助。

正确答案

#1

通常,这是通过返回某个值来完成的,该值允许您决定是否要停止While循环(即某些条件为真还是假):

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanfbagj
系列文章
更多 icon
同类精品
更多 icon
继续加载