Advertisements
Advertisements
Question
The code given below accepts N as an integer argument and returns the sum of all integers from 1 to N. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Comment on all the corrections made.
def Sum(N)
for I in range(N):
S=S+I
return S
print(Sum(10)Code Writing
Advertisements
Solution
def Sum(N): # Correction: Added colon (:)
S = 0 # Correction: Initialized S to 0
for I in range(1,(N+1)): # Correction: Fixed range and added colon (:)
S=S+I
return S # Correction: Proper indentation
print(Sum(10)) # Correction: Added closing bracketshaalaa.com
Is there an error in this question or solution?
2024-2025 (March) Set 4
