0

Sum of consecutive numbers

N = int(input()) sum = 0 for N in range(0, N+1): sum += N print (sum) How can only 1 number display as an answer?

24th Jun 2022, 12:28 PM
Терещенко Дмитрий
Терещенко Дмитрий - avatar
5 Respostas
+ 6
Терещенко Дмитрий Just print outside the loop
24th Jun 2022, 12:36 PM
A͢J
A͢J - avatar
+ 5
A͢J I didn't notice there was a space for indentation Терещенко Дмитрий When asking questions, it is better to link your code as a code bit because it is hard to see proper styling/indentation from plain text. And again, please tag the relevant programming language.
24th Jun 2022, 12:38 PM
Justice
Justice - avatar
+ 5
Терещенко Дмитрий , it is recommended to use indentation as it is described in the python pep 8 - style guide for python code: https://peps.python.org/pep-0008/#introduction -> use 4 spaces per indentation level this will improve the readability of the code and makes it easier to read and understand. 🙂 thanks!
24th Jun 2022, 2:33 PM
Lothar
Lothar - avatar
+ 1
Please be sure to tag the relevant programming language. Also, what is the output you are getting because from first glance, you're only printing one value.
24th Jun 2022, 12:32 PM
Justice
Justice - avatar
0
Терещенко Дмитрий Also, you used the same variable in two places in the for loop: as the loop variable and as end condition. This gives weird results. Pls follow Justice instructions about linking code in the question description and tag the language name. This does help.
25th Jun 2022, 2:49 AM
Emerson Prado
Emerson Prado - avatar