我在 python 中的阶乘代码有什么问题 [英] what is wrong with my factorial code in python
本文介绍了我在 python 中的阶乘代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码用于在 python 中计算数字的阶乘.但我不明白为什么我得到的答案是 1.有人可以纠正我的代码.我想在不使用递归的情况下计算阶乘.
I have the following code for calculating the factorial of a number in python. but I couldnt understand why I am getting the answer as 1. can some one correct my code. I want to calculate the factorial without using recursion.
def factorial (n):
result =1
num = n
while n<1:
result = result * num
num = num -1
return result
factorial(5)
1
推荐答案
while n < 1:
应该是
while num > 1:
这篇关于我在 python 中的阶乘代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文