0
Does anyone know how could I change this code for it to to calculate the factorial of a number?
i = 0 while i < 5: print( i ) i += 1 else: print( "The loop ends, i is now", i ) print( "Done" )
2 odpowiedzi
+ 4
https://code.sololearn.com/c3eg8MvlATAf/?ref=app
https://code.sololearn.com/cOInw4yJq7wi/?ref=app
https://code.sololearn.com/cE7lXk0wwKMN/?ref=app
https://code.sololearn.com/c4phTP2wUQ3u/?ref=app
https://code.sololearn.com/cOfMIY803RdZ/?ref=app
https://code.sololearn.com/c4Q0rFUFxZIP/?ref=app
https://code.sololearn.com/cj7pAcAEmqTm/?ref=app
0
You want to actually save the accumulating values somewhere, say initialize product = 1 before the loop and then inside the loop have product *= i, then print product after the loop.
Changing nothing else but that, your code would give you 4!
I would use a for loop to save one step. And if you know about recursion, one would often approach the problem in that manner.