What is wrong with following function code? Find reason and correct it.
def addThree (n1, n2, n3): return n1 + n2 + n3 print("the answer is", n1 + n2+ n3)
CODE
The issue with the given code is that after returning the sum of the three input numbers
Ans
the function is still trying to print a message to the console.
Ans
Since the return statement is the last statement executed in the function,
Ans
any code written after the return statement will not be executed.
Ans
Therefore, the print statement will never be reached.
Ans
To fix this, simply remove the print statement from the function code as follows:
Ans
def addThree(n1, n2, n3):
return n1 + n2 + n3
Ans
def addThree(n1, n2, n3):
return n1 + n2 + n3
With this modification, the function will correctly return the sum of the three input numbers.
40 facts about outer space | Space Facts
Other Stories
Click Here