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

(a)

total = 0 ;

    def sum( arg1, arg2 ):

    total = arg1 + arg2 ;

    print("Total :", total)

return total ;

sum( 10, 20 ) ;

print("Total :", total)

 

(b)

 

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])

 

Answer =

(i) In line 3 & 4 there should be double indentation.

total = 0
def sum( arg1, arg2 ):
    total = arg1 + arg2
    print("Total :", total)
    return total
sum( 10, 20 )
print("Total :", total)

Output:-

Total : 30

Total : 0


(ii) In line 5 "RETURN" should be in small words and also there is only one indentation.

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))

Output:-

6

21

23 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Thankyou So much..
    ◉‿◉AWESOME SOLUTION

    ReplyDelete
  2. Thank you for such a clear explanation.!

    ReplyDelete
  3. 1st question is from which book

    ReplyDelete
  4. (a) for a part there should be no indent in line 2. there should be no semicolons in line 1 ,3,5 and 6.
    (b) it cant accept a list as argument because it is used in range function. and RETURN.

    ReplyDelete
  5. Ê•⁠ಠ⁠_⁠ಠ⁠Ê”

    ReplyDelete
  6. Not understood the (b) part

    ReplyDelete
    Replies
    1. Please copy the code and run it in your IDLE Python.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post