Computer Science
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"):)
Python Functions
3 Likes
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
- The second return statement in the
alpha
function (return n) is unreachable because the first return statementreturn beta(string)
exits the function. - The function definition lacks colon at the end. The variable
n
in thebeta
function is not defined. It's an argument toalpha
, but it's not passed tobeta
explicitly. To accessn
withinbeta
, we need to either pass it as an argument or define it as a global variable. - There should not be colon at the end in the function call.
- In the function call
beta(string = 'true')
, there should be argument for parametern
. - 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"))
Answered By
2 Likes
Related Questions
Find the errors in code given below :
def minus(total, decrement) output = total - decrement print(output) return (output)
Find the errors in code given below :
define check() N = input ('Enter N: ') i = 3 answer = 1 + i ** 4 / N Return answer
Draw the entire environment, including all user-defined variables at the time line 10 is being executed.
1. def sum(a, b, c, d): 2. result = 0 3. result = result + a + b + c + d 4. return result 5. 6. def length(): 7. return 4 8. 9. def mean(a, b, c, d): 10. return float(sum(a, b, c, d))/length() 11. 12. print(sum(a, b, c, d), length(), mean(a, b, c, d))
Draw flow of execution for the above program.