Factorial number python

Report
Posts
1
Registration date
Friday August 2, 2019
Status
Member
Last seen
August 2, 2019
-
 Lameidiot5 -
Hello,

hi don't understand why this code doesn't give me the factorial number.

somma = 0
i = 1
num = getInteger("Inserisci un numero e ti dirò il fattoriale: ")

if num == 1:
print("Il fattoriale è 1 ")
else:
while i < num:
somma = i * num
print("Il risultato è: ", somma)

2 replies

Posts
5
Registration date
Monday December 23, 2019
Status
Member
Last seen
January 14, 2020

Why do you not reduce 'num' in the "while" cycle?
Just declare somma do not initialize it as zero. "I" is always one. You have a while statement that checks against a static variable. Get rid of the while and calculate it directly as previously stated. If "I" was an array, such as 1,3,5,7,9, then a while would be applicable, but it is not an array, it is static.