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!
6 Respostas
+ 1
as you have your print indented (with leading spaces), it is in your for loop ;)
+ 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)
0
desindent the line where you print(sum)...
0
what do you mean by that? sorry if i ask too much! ;<
0
???
can u give me an example?
0
Thank you!!!!!