【问题标题】:Python why i got no output herePython为什么我在这里没有输出
【发布时间】:2018-05-31 14:15:42
【问题描述】:

我最近开始学习 python,但我不明白为什么下面的代码没有输出:

def countdown():
    i = 5
    while i > 0:
        return i
        i -= 1
    print (i)

【问题讨论】:

  • return i 在第一次迭代结束前退出

标签: python function return yield


【解决方案1】:

正如 @alfasin 在 cmets 中所述,您可以在函数执行任何操作之前使用 return 退出函数。

你可能打算这样做:

def countdown():
    i = 5
    while i > 0:
        print(i)
        i -= 1

    return i

然后调用函数:

countdown()

输出:

5
4
3
2
1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多