Q.Observe the following programs carefully, and identify the error:
def create (text, freq):
for i in range (1, freq):
print text
create(5) #function call
from math import sqrt,ceil
def calc():
print cos(0)
calc() #function call
mynum = 9
def add9():
mynum = mynum + 9
print mynum
add9() #function call
def findValue( vall = 1.1, val2, val3):
final = (val2 + val3)/ vall
print(final)
findvalue() #function call
def greet():
return("Good morning")
greet() = message #function call
The five snippets showcase five classic function errors: Python-2 print syntax with a missing argument (a), an unimported name (b), the local-shadows-global trap (c), a default parameter placed before non-default ones (d), and assigning to a function call (e).
The idea. Debugging function code means checking four things systematically: the syntax of each statement, the names in scope, the parameter list rules, and the call itself (name, argument count, what the result is assigned to). Each sub-part breaks exactly one of these.
a) print text + create(5)
def create (text, freq):
for i in range (1, freq):
print text
create(5)
- Error 1 (reported first):
print textis Python-2 statement syntax. Python 3 raisesSyntaxError: Missing parentheses in call to 'print'— the program never even starts. - Error 2 (after fixing that):
create(5)supplies only one argument for two required parameters →TypeError: create() missing 1 required positional argument: 'freq'. - Corrected:
def create(text, freq):
for i in range(1, freq):
print(text)
create("hello", 5) # prints "hello" 4 times (range(1,5))
b) print cos(0) with the wrong imports
from math import sqrt,ceil
def calc():
print cos(0)
calc()
- Error 1: again the Python-2
printstatement →SyntaxError. - Error 2: only
sqrtandceilwere imported;cosis undefined →NameError: name 'cos' is not definedonce the syntax is fixed. - Corrected:
from math import sqrt, ceil, cos
def calc():
print(cos(0))
calc()
1.0
c) Modifying a global inside a function
mynum = 9
def add9():
mynum = mynum + 9
print mynum
add9()
- Besides the
printsyntax issue, the real lesson: becausemynumis assigned inside the function, Python treats it as a local variable throughout the function — somynum + 9reads a local that has no value yet →UnboundLocalError: local variable 'mynum' referenced before assignment. - Corrected with a
globaldeclaration:
mynum = 9
def add9():
global mynum
mynum = mynum + 9
print(mynum)
add9()
18
d) Default parameter before non-default ones
def findValue( vall = 1.1, val2, val3):
final = (val2 + val3)/ vall
print(final)
findvalue()
- Error 1:
SyntaxError: non-default argument follows default argument— once a parameter has a default (vall = 1.1), every parameter after it must also have one. - Error 2: the call says
findvalue()but the function isfindValue— Python names are case-sensitive →NameError. (And evenfindValue()would need values forval2,val3.) - Corrected:
def findValue(val2, val3, vall = 1.1):
final = (val2 + val3) / vall
print(final)
findValue(5.5, 6.6)
10.999999999999998
(Not a clean 11.0 — 6.6 and 1.1 have no exact binary representation, so float arithmetic leaves a tiny rounding tail. This is normal Python behaviour, not a bug in the fix.)
e) Assigning to a function call
def greet():
return("Good morning")
greet() = message
- The assignment is backwards:
greet() = messagetries to use a function call as the assignment target →SyntaxError: cannot assign to function call. (messageis also undefined.) - Corrected:
def greet():
return "Good morning"
message = greet()
print(message)
Good morning
Python reports syntax errors before running anything, so in (a), (b) and (d) you see only the SyntaxError at first; the TypeError/NameError surfaces only after the syntax is repaired. Fix errors in that order.
a) Python-2 print (SyntaxError) + call missing the second argument (TypeError). b) Python-2 print + cos not imported (NameError). c) UnboundLocalError — assignment makes mynum local; needs global mynum. d) SyntaxError — non-default parameters cannot follow a default one; call also misspells the case-sensitive name. e) SyntaxError — cannot assign to a function call; must be message = greet().
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.