CBSE Class 11 Computer Science Question 127 of 173

Data Handling — Question 16

Back to all questions
16
Question

Question 12

Consider the following program. It is supposed to compute the hypotenuse of a right triangle after the user enters the lengths of the other two sides.

a = float(input("Enter the length of the first side:"))
b = float(input("Enter the length of the second side:"))
h = sqrt(a * a + b * b)
print("The length of the hypotenuse is", h)

When this program is run, the following output is generated (note that input entered by the user is shown in bold):

Enter the length of the first side: 3
Enter the length of the second side: 4
Traceback (most recent call last):
h = sqrt(a * a + b * b)
NameError: name 'sqrt' is not defined

Why is this error occurring? How would you resolve it ?

Answer

The error is coming because math module is not imported in the code. To resolve it, we should import the math module using the import statement import math.

Answer