0

whats wrong with my code???

In mathematics, the factorial of an integer n, denoted by n! Warning: DON'T USE MATH MODULE!!!!! This is my code: n = int(input()) sum = 1 for i in range(1, n+1): sum = sum * i print(sum) why does my cod output the other thing? For example when I run my codeI enter 4 in the terminal and the output should be 24! You can check my code here in the link: https://snakify.org/en/lessons/for_loop_range/problems/factorial/ WARNING: DON'T TELL ME THE ANSWER! JUST EXPLAIN TO ME WHY DOES IT OUTPUT THE OTHER THING! PLEASE!

12th Feb 2021, 5:51 AM
Ailana
Ailana - avatar
6 Respostas
+ 1
as you have your print indented (with leading spaces), it is in your for loop ;)
12th Feb 2021, 6:10 AM
visph
visph - avatar
+ 1
for i in range(1,1+n): sum = sum * i print(sum) # for n = 4 would output: 1 2 6 24 # but you need only the last one: for i in range(1,1+n): sum = sum * i print(sum)
12th Feb 2021, 6:16 AM
visph
visph - avatar
0
desindent the line where you print(sum)...
12th Feb 2021, 5:53 AM
visph
visph - avatar
0
what do you mean by that? sorry if i ask too much! ;<
12th Feb 2021, 6:08 AM
Ailana
Ailana - avatar
0
??? can u give me an example?
12th Feb 2021, 6:13 AM
Ailana
Ailana - avatar
0
Thank you!!!!!
12th Feb 2021, 6:19 AM
Ailana
Ailana - avatar