Computer Science
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()
Related Questions
A program having multiple functions is considered better designed than a program without any functions. Why ?
Write a module called calculate_area() that takes base and height as an input argument and returns an area of a triangle as output. The formula used is
Triangle Area = 1⁄2 * base * height
How is math.ceil(89.7) different from math.floor(89.7)?
Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.