+ 5
Please help how this code works.[Python]
This code prints 120 and I dunno why? https://code.sololearn.com/cjRN9d3eDfLI/?ref=app
3 Antworten
+ 11
That function returns the factorial of a number (n!), in that case 5.
So:
n * (n - 1)
n = 5
5 * (5 - 1) * (4 - 1) * (3 - 1) * ( 2 - 1)
= 5 * 4 * 3 * 2 *1
= 120
It's also explained here:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2465/
+ 6
What I think:
When n = 5
The last line of the fact() function runs and returns=
5 * fact(4) -> 4 * fact(3) -> 3 * fact(2) -> 2 * fact(1) -> 1 * fact(0) -> 1
The code prints 120 maybe because 5*4*3*2*1=120, but still I'm not cleared about this, so can you please elaborate.
+ 5
Koketso Dithipe thank you