Skip to content
Exercises · Q1

Q.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
print mynum
add9() #function call
d)
def findValue( vall = 1.1, val2, val3):
final = (val2 + val3)/ vall
print(final)
findvalue() #function call
e)
def greet():
return("Good morning")
greet() = message #function call
Chhattisgarh CgbseTextbookSubjective· 3mImportance★★★★★est
48% · 19/40 Questions
✓ Free question

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 text is Python-2 statement syntax. Python 3 raises SyntaxError: 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 print statement → SyntaxError.
  • Error 2: only sqrt and ceil were imported; cos is undefined → NameError: name 'cos' is not defined once 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 print syntax issue, the real lesson: because mynum is assigned inside the function, Python treats it as a local variable throughout the function — so mynum + 9 reads a local that has no value yet → UnboundLocalError: local variable 'mynum' referenced before assignment.
  • Corrected with a global declaration:
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 is findValue — Python names are case-sensitive → NameError. (And even findValue() would need values for val2, 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() = message tries to use a function call as the assignment target → SyntaxError: cannot assign to function call. (message is also undefined.)
  • Corrected:
def greet():
    return "Good morning"
message = greet()
print(message)
Good morning
Watch out

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.

✓Final answer

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.