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)
5 odpowiedzi
+ 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
+ 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.
+ 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.
+ 1
Thanks guys!
0
Thank you so much!