What is the problem in this code defined for factorial function? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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))

18th Aug 2019, 5:24 AM
Avinash
Avinash - avatar
2 Réponses
+ 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)
18th Aug 2019, 7:07 AM
Trigger
Trigger - avatar
+ 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
18th Aug 2019, 5:40 AM
Ipang