Computer Science
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)
After adding import math to the code given above, what other change(s) are required in the code to make it fully work ?
Python Data Handling
15 Likes
Answer
After adding import math statement, we need to change the line h = sqrt(a * a + b * b)
to h = math.sqrt(a * a + b * b)
. The corrected working code is below:
import math
a = float(input("Enter the length of the first side:"))
b = float(input("Enter the length of the second side:"))
h = math.sqrt(a * a + b * b)
print("The length of the hypotenuse is", h)
Answered By
10 Likes
Related Questions
Following expression does not report an error even if it has a sub-expression with 'divide by zero' problem:
3 or 10/0
What changes can you make to above expression so that Python reports this error?
Find out the error and the reason for the error in the following code. Also, give the corrected code.
a, b = "5.0", "10.0" x = float(a/b) print(x)
Which of the following expressions will result in an error message being displayed when a program containing it is run?
(a) 2.0/4
(b) "3" + "Hello"
(c) 4 % 15
(d) int("5")/float("3")
(e) float("6"/"2")
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 definedWhy is this error occurring? How would you resolve it ?