(Solved) Why my code doesn't work????🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

(Solved) Why my code doesn't work????🤔

N=int(input()) sum=0 for i in range(N+1): sum+=i print(sum)

8th Apr 2024, 7:17 AM
Ayda Tazh
Ayda Tazh - avatar
9 Answers
+ 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)
8th Apr 2024, 7:43 AM
Rain
Rain - avatar
+ 1
Can you share the input and expected output then we can modify your code accordingly
9th Apr 2024, 9:39 AM
Kailash Yandrapu
+ 1
Ok
9th Apr 2024, 11:35 AM
Kailash Yandrapu
+ 1
It does work with N same as n N=int(input()) sum=0 for i in range(N+1): sum+=i print(sum)
9th Apr 2024, 2:58 PM
Suhani Kewat
Suhani Kewat - avatar
+ 1
Ok 👌🏽
9th Apr 2024, 3:45 PM
Ayda Tazh
Ayda Tazh - avatar
0
I sloved it, thanks
9th Apr 2024, 10:10 AM
Ayda Tazh
Ayda Tazh - avatar
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/
9th Apr 2024, 3:29 PM
Rain
Rain - avatar
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) }
9th Apr 2024, 3:32 PM
Omkar Arundekar
Omkar Arundekar - avatar
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?
9th Apr 2024, 3:41 PM
Rain
Rain - avatar