0

Explain this python code, how it results 7?

def fact(x): If x ==1: return 2 else: return x + fact(x - 1) print(fact(3))

19th Apr 2020, 11:44 AM
Rohit Singh
Rohit Singh - avatar
6 Respuestas
+ 1
>>>fact(3) >>>3 + fact(2) #3 >>>2 + fact(1) #2 x is now == 1 so... 2 #2 The sum of all the iterations (the numbers after the comments) is 7.
19th Apr 2020, 12:00 PM
Slick
Slick - avatar
+ 2
fact3 = 3 + fact2 = 3+2+fact1 = 3+2+2=7
19th Apr 2020, 12:00 PM
Oma Falk
Oma Falk - avatar
0
Isaac Clarke what do you mean?
20th Apr 2020, 7:39 AM
Oma Falk
Oma Falk - avatar
0
Thanks Slick and Oma Fold
22nd Apr 2020, 10:07 AM
Rohit Singh
Rohit Singh - avatar