Typeerror: Unsupported Operand Type(s) For ** Or Pow(): 'str' And 'int'
I am just experimenting and having fun with Python 2.7 and I'm trying to write a quadratic equation solver. I had it working when the radicand is positive, but when it's negative i
Solution 1:
You should replace:
float(a)
with:
a = float(a)
and similarly for the other variables.
The statement float(a)
doesn't actually turn a
into a float
, it simply casts it, as per the following transcript:
>>>a = raw_input("a? ")
a? 4.5
>>>type(a)
<type 'str'>
>>>float(a)
4.5
>>>type(a)
<type 'str'>
>>>a = float(a)>>>type(a)
<type 'float'>
You can see that the type of a
is stillstr
immediately after performing float(a)
but, when you execute thew assignment a = float(a)
, the type becomes float
.
Solution 2:
the statement float(a)
returns the float value of the string a. But if you do not assign to any other variable, then float(a) holds only for that particular line. In the next line, a is a string. So, assign the float value to a variable, say a itself.
a=float(a)
This will make a
a float
Post a Comment for "Typeerror: Unsupported Operand Type(s) For ** Or Pow(): 'str' And 'int'"