Python 为什么我这里没有输出

Python why i got no output here

我最近开始学习 python,我不明白为什么我没有从下面的代码中得到任何输出:

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

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

您可能打算这样做:

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

    return i

然后调用函数:

countdown()

输出:

5
4
3
2
1