Python为什么我在这里没有输出

问题描述 投票:1回答:1

我最近开始学习python,但无法弄清楚为什么下面的代码没有输出:

def countdown():
    i = 5
    while i > 0:
        return i
        i -= 1
    print (i)
python function return yield
1个回答
1
投票
如注释中的@alfasin所述,您在函数执行任何操作之前先使用return退出了该函数。

您可能打算这样做:

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

然后调用函数:

countdown()

输出:

5 4 3 2 1

© www.soinside.com 2019 - 2024. All rights reserved.