+ 5
(Solved) Why my code doesn't work????🤔
N=int(input()) sum=0 for i in range(N+1): sum+=i print(sum)
9 ответов
+ 6
Ayda Tazh ,
It does work (assuming you input a non-negative integer). I inputted 5, and it printed 15, which is the sum of 0 + 0 + 1 + 2 + 3 + 4 + 5.
But you could make it look better. Variable names should be lowercase. Operators should be surrounded by single spaces. Indentation should be four spaces per level.
n = int(input())
sum = 0
for i in range(n + 1):
sum += i
print(sum)
+ 2
It does work with N same as n
N=int(input())
sum=0
for i in range(N+1):
sum+=i
print(sum)
+ 1
Can you share the input and expected output then we can modify your code accordingly
+ 1
Ok
+ 1
Ok 👌🏽
0
I sloved it, thanks
0
Suhani ,
Yes, N is a legal name, so the code runs, but the naming convention is that variable names should use snake_case,
https://peps.python.org/pep-0008/#function-and-variable-names
and class names should use CapitalizedWords.
https://peps.python.org/pep-0008/#class-names
There are more naming conventions too. Here's a good third-party table that summarizes them (rotate the phone to landscape aspect).
https://namingconvention.org/python/
0
Try adding the if loop to n must be greater than or equal to 0
n=int(input ())
s=0
if n>=0:
{
for i in range(n+1) :
s+=i
print(sum)
}
0
Ayda Tazh ,
If it's solved, will you add [Solved] to the title so people reading the list of titles know they don't need to help?