计算阶乘的程序为阶乘1003生成6的输出

2024-09-30 23:35:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,用于计算阶乘

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))

例如阶乘5=120。这使用下面的代码工作

然而,当我使用pythonidle输入“1003”作为输入时,输出是6

有人能解释一下吗?下面是截图

重要提示:仅当我运行了两个测试时才会发生:

例如,试试999、1000、9000,然后再试试1003。在最后一次测试中,6作为输出出现。空闲时发生了什么导致这种情况

enter image description here


Tags: theto代码numberinputreturnifdef