+ 2
What is the problem in this code defined for factorial function?
Code is Def factorial (x): While x > 0: x *= x x -= 1 Return x print(factorial(10))
2 odpowiedzi
+ 2
Because you are using "x * x" and "x -= x" you are constantly changing that same variable with "no constant"
This will work better
def Factorial(x):
i = x
while i > 0:
x *= i
i -= 1
return x
You could also do it recursively
def Factorial(x):
if x == 1:
return x
return x*Factorial(x-1)
+ 3
Please do not write a sentence into the Relevant Tags section. Have a look at the following discussion to understand 👍
https://www.sololearn.com/Discuss/333866/?ref=app