0
Anybody with understanding of this code should explain it please.
def factorial(num): fact = 1 while num > 0: fact = fact * num num = num -1 return fact print(factorial(4))
5 Réponses
+ 3
This is a recursive function that start from the num param given as 4 then check condition 4 > 0 so substract 1 from num and multiply it by first value of num 4
After looping every time the condition num>0 is true the function return num×num-1 or here 4*3*2*1 == 24
+ 3
Yea I got the real solution
def factorial(num):
fact = 1
while num > 0:
fact = fact * num
num = num -1
return fact
print(factorial(4))
########################I think this is the format of this code.
4 has been assigned to num I.e.
num = 4. then
fact = 1. and the condition is given to be :
num > 0 and again "fact" is multiplied by "num" i.e.
fact = 1*4 and
num = 4 - 1 then
return fact which is 1*4 = 4
THE PROGRAM WILL LOOP AGAIN like this
num = 3
fact = 4
while num > 0
fact = 4*3
num = 3 -1
return fact which is 4*3 = 12
THE PROG WILL LOOP AGAIN UNTIL num =1 : final ans=24
+ 2
Ya clear it is not recursive it does not call to it self but there is a while loop which do the whole task .Thanks rodwynnejones .
+ 1
Wao! thanks so much. I got it now
+ 1
@HBhZ_C ....recursive?.....look at the code again.