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 in math 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, causing UnboundLocalError 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

Was this helpful?

India's 1st AI Doubt Solver for CBSE, JEE, and NEET

Ask a Question for Free

and then it's just ₹212 a month

Learn how Chatterbot AI can help you succeed

Cover Image of the article Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

This ultimate guide for CBSE Computer Science class 11 has detailed notes, NCERT solutions, cheat sheets, and our free AI-powered doubt-solving assistant, Chatterbot AI.

Cover Image of the article JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 on May 26! Get exam schedule, syllabus, prep tips & more in this guide. Ace India's top engineering test with topper strategies.

Cover Image of the article How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

Ace NEET with expert strategies: Discover effective exam strategies, time management, core concepts mastery, problem-solving techniques, revision tips, and AI-assisted doubt clearing with Chatterbot AI.

Cover Image of the article How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

Ace IIT JEE in 6 months without coaching. Discover expert self-study strategies for Physics, Chemistry, and Math. Master time management, mock tests, and leverage AI tools like Chatterbot AI for personalized doubt-solving.