CBSE Class 11 Computer Science Question 42 of 63

Introduction to Python Modules — Question 11

Back to all questions
11
Question

Question 11

Rewrite the following Python code after removing all syntax error(s). Underline the corrections done.

def main():
    r = input('enter any radius:')
    a = pi * maths.pow(r, 2)
    Print("Area = "+a)
        Main()
Answer
def main():
    r = input('enter any radius:') #Error 2
    a = pi * maths.pow(r, 2) #Error 3
    Print("Area = "+a) #Error 4
        Main() #Error 5

Error 1 — The import math statement is missing.

Error 2 — The input() function returns a string, but the radius should be a floating-point number, so added float() function to convert it.

Error 3 — The maths module is incorrect, so changed it to math and added math. before pi to correctly reference the mathematical constant and function.

Error 4 — The Print function is incorrect, so changed it to print. Instead of using + operator, a comma (,) is used to print the string followed by value of a.

Error 5 — The function name Main is incorrect, so changed it to main() to match the function definition and the indentation was corrected to ensure proper execution.

The corrected code is as follows:

import math

def main():
    r = float(input('enter any radius:')) 
    a = math.pi * math.pow(r, 2)
    print("Area = ", a) 
main()