Computer Science
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)
Python Data Handling
23 Likes
Answer
a and b are defined as strings not float or int. Division operator doesn't support strings as its operand so we get the error — unsupported operand type(s) for /: "str" and "str".
The corrected code is:
a, b = 5.0, 10.0
x = float(a/b)
print(x)
Answered By
12 Likes
Related Questions
Consider the following code segment:
a = input("Enter the value of a:") b = input("Enter the value of b:") print(a + b)
If the user runs the program and enters 11 for a and 9 for b then what will the above code display?
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 ?
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 ?
Consider the following code segment:
a = input() b = int(input()) c = a + b print(c)
When the program is run, the user first enters 10 and then 5, it gives an error. Find the error, its reason and correct it