简体   繁体   English

类型错误:** 或 pow() 不支持的操作数类型:“str”和“int”

[英]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.我只是在试验 Python 2.7 并从中获得乐趣,我正在尝试编写一个二次方程求解器。 I had it working when the radicand is positive, but when it's negative im getting an error.当 radicand 为正时我让它工作,但当它为负时我得到一个错误。 even after this if else statement.即使在这个 if else 语句之后。 it also doesnt work with big numbers.它也不适用于大数字。 thanks for the help.谢谢您的帮助。

import math
a = raw_input("a = ")
b = raw_input("b = ")
c = raw_input("c = ")
float(a)
float(b)
float(c)
radicand = ((b**2)-4*a*c)
if radicand >= 0:
    print(((0-b) + math.sqrt((b**2)-4*a*c))/(2*a))
    print(((0-b) - math.sqrt((b**2)-4*a*c))/(2*a))
else:
    print "Imaginary Radical"

when i replace (b**2)-4*a*c with radicand i get an invalid syntax error and print is highlighted in red.当我用 radicand 替换 (b**2)-4*a*c 时,我得到一个无效的语法错误并且打印以红色突出显示。 the error message says TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'错误信息说 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
thanks again for any insight you can provide...再次感谢您提供的任何见解...

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:语句float(a)实际上并没有将a变成float ,它只是根据以下抄本对其进行转换:

>>> 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 still str immediately after performing float(a) but, when you execute thew assignment a = float(a) , the type becomes float .你可以看到在执行float(a)之后a的类型仍然str ,但是当你执行 w 赋值a = float(a)时,类型变成了float

the statement float(a) returns the float value of the string a.语句float(a)返回字符串 a 的浮点值。 But if you do not assign to any other variable, then float(a) holds only for that particular line.但是,如果您不分配给任何其他变量,则 float(a) 仅适用于该特定行。 In the next line, a is a string.在下一行中,a 是一个字符串。 So, assign the float value to a variable, say a itself.因此,将浮点值分配给一个变量,比如 a 本身。

a=float(a)

This will make a a float这将使a浮动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3 TypeError:**或pow()不支持的操作数类型:“ str”和“ int” - Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' TypeError:|:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'int' and 'str' TypeError:/:&#39;str&#39;和&#39;int&#39;的不支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'str' and 'int' TypeError:不支持的操作数类型/:'str'和'int' - TypeError: unsupported operand type(s) for /: 'str' and 'int' for TypeError: %: 'str' 和 'int' 不支持的操作数类型 - TypeError: unsupported operand type(s) for %: 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM