Reputation: 13
I am new to coding and I have no idea how to use 'floats'. Could someone please inform me where to use floats in this piece of code so that I will not receive the:
TypeError, 'float' object is not callable.
my code so far:
def problem1_7():
b1 = input("Enter the length of one of your bases: ")
b2 = input("Enter the length of one of your bases: ")
h = input("Enter the height: ")
area = (1/2)(b1+b2)*h
print("The area of a trapezoid with bases",b1,"and",b2,"and height ",h,"is",area)
Upvotes: 0
Views: 486
Reputation: 74
The problem is in area = (1/2)(b1+b2)*h
.
Python syntax differs from a mathematical, for one, () are used to pass parameters to a method call. However, (1/2) returns a float, hence what is interpreted is a call with the result of b1 + b2 as parameter to (1/2).
You just need to insert *
in the formula to make it right, i.e. change method invocation to multiplication (which is defined for type float).
Hence, the fixed line would be area = (1/2)*(b1+b2)*h
.
Another issue, you would notice after fixing this is the actual type of all variables , i.e. b1
, b2
, and h
being strings. To fix that you need to parse them as floats:
b1 = float(input("Enter the length of one of your bases: "))
b2 = float(input("Enter the length of one of your bases: "))
h = float(input("Enter the height: "))
Python uses duck typing, hence problem such as those often surface in run time.
Upvotes: 2