My code:
total=tef+tpf-price
I've got this error:
total=tef+tpf-price
unsupported operand type(s) for -: 'float' and 'str'
How do I fix it?
My code:
total=tef+tpf-price
I've got this error:
total=tef+tpf-price
unsupported operand type(s) for -: 'float' and 'str'
How do I fix it?
One simple way to fix it is:
Instead of this
Try this, I hope this will help you
The only way that error could occur is if
price
is a string. Make price a float or an integer (depending on what you want) to fix the problem.Either this:
or this:
Notice that, in Python, to preform an operation between two objects, those object must be of the same type (and support the operation of course).
I think you might take user's
price
input, like:or
So you might want to do some validation before using it.
You could cast
price
from string to float byfloat(price)
.