CBSE Class 12 Computer Science Question 85 of 103

Working with Functions — Question 18

Back to all questions
18
Question

Question 12(c)

Find the errors in code given below :

def alpha (n, string = 'xyz', k = 10) :
    return beta(string)
    return n

def beta (string)
    return string == str(n)

print(alpha("Valentine's Day"):)
print(beta(string = 'true'))
print(alpha(n = 5, "Good-bye"):)
Answer

The errors in the code are:

def alpha (n, string = 'xyz', k = 10) :
    return beta(string)
    return n #Error 1

def beta (string) #Error 2
    return string == str(n)

print(alpha("Valentine's Day"):) #Error 3
print(beta(string = 'true')) #Error 4
print(alpha(n = 5, "Good-bye"):) #Error 5
  1. The second return statement in the alpha function (return n) is unreachable because the first return statement return beta(string) exits the function.
  2. The function definition lacks colon at the end. The variable n in the beta function is not defined. It's an argument to alpha, but it's not passed to beta explicitly. To access n within beta, we need to either pass it as an argument or define it as a global variable.
  3. There should not be colon at the end in the function call.
  4. In the function call beta(string = 'true'), there should be argument for parameter n.
  5. In the function call alpha(n = 5, "Good-bye"), the argument "Good-bye" lacks a keyword. It should be string = "Good-bye".

The corrected code is given below:

def alpha(n, string='xyz', k=10):
    return beta(string, n)

def beta(string, n):
    return string == str(n)

print(alpha("Valentine's Day"))
print(beta(string='true', n=10))
print(alpha(n=5, string="Good-bye"))