Observe the following programs carefully, and identify the error: a) def create (text, freq): for i in range (1, freq): print text create(5) # function call b) from math import sqrt, ceil def calc(): print cos (0) calc() #function call c) mynum = 9 def add9(): mynum = mynum + 9 add9() # function call d) def findValue(val1 = 1.1, val2, val3): final = (val2 + val3) / vall1 print(final) findValue() # function call e) def greet(): return("Good morning") greet() = message # function call
Question
Observe the following programs carefully, and identify the error:
a)
def create (text, freq):
for i in range (1, freq):
print text
create(5) # function call
b)
from math import sqrt, ceil
def calc():
print cos (0)
calc() #function call
c)
mynum = 9
def add9():
mynum = mynum + 9
add9() # function call
d)
def findValue(val1 = 1.1, val2, val3):
final = (val2 + val3) / vall1
print(final)
findValue() # function call
e)
def greet():
return("Good morning")
greet() = message # function call
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Let's identify the errors in each of the provided programs:
a)
def create (text, freq):
for i in range (1, freq):
print text
create(5) # function call
Error:
Missing parentheses in the
print
statement (Python 3).Incorrect number of arguments passed to
create
.
Fix:
def create(text, freq):
for i in range(1, freq):
print(text)
create("Sample Text", 5) # function call
b)
from math import sqrt, ceil
def calc():
print cos(0)
calc() # function call
Error:
Missing import for
cos
function inmath
module.Missing parentheses for
print
(Python 3).
Fix:
from math import sqrt, ceil, cos
def calc():
print(cos(0))
calc() # function call
c)
mynum = 9
def add9():
mynum = mynum + 9
add9() # function call
Error:
mynum
inside the function is considered local, causingUnboundLocalError
because it is being used before assignment.
Fix:
mynum = 9
def add9():
global mynum
mynum = mynum + 9
add9() # function call
d)
def findValue(val1 = 1.1, val2, val3):
final = (val2 + val3) / val1
print(final)
findValue() # function call
Error:
Non-default arguments (
val2
,val3
) cannot follow default arguments (val1
).
Fix:
def findValue(val2, val3, val1=1.1):
final = (val2 + val3) / val1
print(final)
findValue(1, 2) # function call
e)
def greet():
return "Good morning"
greet() = message # function call
Error:
Incorrect assignment syntax.
greet()
returns a value, it cannot be assigned to.
Fix:
def greet():
return "Good morning"
message = greet() # function call
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month