gpt4 book ai didi

python - 在 Python 中返回外部函数错误

转载 作者:行者123 更新时间:2023-11-28 19:40:09 28 4
gpt4 key购买 nike

问题是:给定以下 Python 程序,假设用户从键盘输入数字 4。返回的值是多少?

N = int(input("enter a positive integer:"))
counter = 1
while (N > 0):
counter = counter * N
N = N - 1
return counter

但我在运行系统时总是出现外部函数错误我究竟做错了什么?谢谢!

最佳答案

您只能从函数内部返回,而不能从循环中返回。

看起来你的 return 应该在 while 循环之外,而你的完整代码应该在一个函数内。

def func():
N = int(input("enter a positive integer:"))
counter = 1
while (N > 0):
counter = counter * N
N -= 1
return counter # de-indent this 4 spaces to the left.

print func()

如果这些代码不在函数内,那么您根本不需要return。只需在 while 循环 外打印 counter 的值。

关于python - 在 Python 中返回外部函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924820/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com