Whats wrong with this code? | Sololearn: Learn to code for FREE!


Whats wrong with this code?

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

4/7/2021 7:32:46 AM

Sajjad Samiei naserani

6 Answers

New Answer


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


Can u please tell that whats ur desired output. As while running this code runs without an error.


Here's a O(1) N = int(input()) print((N/2)*(N+1))


You aren't adding the numbers together in the loop and then output the total after the loop, but just outputting each number in the range. Note: you shouldn't use 'sum' as a variable name, as it will overwrite the built-in sum() functions name.


Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050


print(sum(range(int(input()) + 1)))