0

What am I doing wrong? For Sum of consecutive numbers

N = int(input()) #your code goes here list = list(range(1, N + 1)) sum = 0 for num in range(1, N + 1): sum += num print(sum)

24th Feb 2023, 11:05 PM
The Tina Experience
The Tina Experience - avatar
5 Respuestas
+ 8
>>> please avoid using reserved names from built-in functions or other objects when creating codes. keywords that are frequently used as variable names: id, list, dict, input, sum, min, max, ... <= these are all reserved keywords in python >>> this can lead to a unexpected behavior or to an error. the reason is (if we use reserved words), that the corresponding built-in function can be shadowed or will be overwritten. > in pep 8 - (style guide for python code) we can find this in section: > descriptive: naming styles : single_trailing_underscore_ : ***used by convention to avoid conflicts with python keywords*** e.g.: sum_ = result1 + result2
26th Feb 2023, 11:58 AM
Lothar
Lothar - avatar
+ 4
Code work fine, it print sum of numbers in range, but because you set print inside for loop it print each iteration. So if you wanna print total sum, place print outside of for loop. Also you never used list variable, you repeated same code inside loop, so replace range inside loop to list or remove list variable completely. If this does not help, please describe problem more.
24th Feb 2023, 11:14 PM
PanicS
PanicS - avatar
+ 3
Side note: never use existing functions, types, classes, etc., as variable names. In any language. This can get you bugs almost impossible to debug.
26th Feb 2023, 11:09 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Thanks guys!
26th Feb 2023, 12:20 PM
The Tina Experience
The Tina Experience - avatar
0
Thank you so much!
24th Feb 2023, 11:16 PM
The Tina Experience
The Tina Experience - avatar