Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何得到一个while循环,该循环计算阶乘以忽略前面的阶乘?_Python_While Loop_Factorial - Fatal编程技术网

Python 如何得到一个while循环,该循环计算阶乘以忽略前面的阶乘?

Python 如何得到一个while循环,该循环计算阶乘以忽略前面的阶乘?,python,while-loop,factorial,Python,While Loop,Factorial,更新:我通过在循环开始时将阶乘重置为1并消除不必要的代码,解决了复合阶乘的问题。这是新版本 more = "yes" while more == "yes": n = int(input("Enter a number to get its factorial: ")) if n >= 0: factorial = 1 for num in range(1, n+1): factorial *= num

更新:我通过在循环开始时将阶乘重置为1并消除不必要的代码,解决了复合阶乘的问题。这是新版本

more = "yes"
while more == "yes":
    n = int(input("Enter a number to get its factorial: "))
    if n >= 0:
        factorial = 1
        for num in range(1, n+1):
            factorial *= num
        print(n,'!=', factorial)
        more = input("Would you like to get another number's factorial?:")
    else:
        print("Factorials are not defined for negative integers.")
print("Thank You!")

您需要在while循环开始时将阶乘变量重置为1


此外,您还需要检查n是否对每个输入都为正,因为它目前只对第一个输入为正。

谢谢Dima!我解决了复合问题,但现在我遇到了一个问题,它显示输入的负整数等于一的阶乘,然后要求另一个整数。这就是我所拥有的…代码可以简单得多,这将帮助您解决问题。首先,将所有内容都放在while循环中,并且在循环开始时,只在代码中有一次要求数字的代码。非常感谢Dima,我解决了这个问题!你是最棒的!更新:我已经修复了这个问题并简化了代码。