KnowledgeBoat Logo

Computer Science

What are the errors in following codes ? Correct the code and predict output :

def Tot(Number) #Method to find Total
    Sum = 0
    for C in Range (1, Number + 1): 
        Sum += C
        RETURN Sum
print (Tot[3])  #Function Calls
print (Tot[6])

Python

Python Functions

11 Likes

Answer

def Tot(Number): #Method to find Total
    Sum = 0
    for C in range(1, Number + 1): 
        Sum += C
    return Sum
print(Tot(3))  #Function Calls
print(Tot(6))
6
21

Working

  1. There should be a colon (:) at the end of the function definition line to indicate the start of the function block.
  2. Python's built-in function for generating sequences is range(), not Range().
  3. Python keywords like return should be in lowercase.
  4. When calling a function in python, the arguments passed to the function should be enclosed inside parentheses () not square brackets [].

Answered By

2 Likes


Related Questions