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?
5 Réponses
+ 6
Терещенко Дмитрий
Just print outside the loop
+ 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.
+ 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!
+ 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.
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.