Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python返回TypeError?_Python - Fatal编程技术网

为什么python返回TypeError?

为什么python返回TypeError?,python,Python,我必须做一个程序,使用二次公式和用户提供的a、b和c的值 代码如下: import math from math import sqrt from math import pow #input values a = input('Value for a: ') b = input('Value for b: ') c = input('Value for c: ') #calculate the discriminant d = (b**2) - (4*a*c) if d < 0:

我必须做一个程序,使用二次公式和用户提供的a、b和c的值

代码如下:

import math
from math import sqrt
from math import pow

#input values
a = input('Value for a: ')
b = input('Value for b: ')
c = input('Value for c: ')

#calculate the discriminant
d = (b**2) - (4*a*c)

if d < 0:
    d *= -1
else:
    d=d

#Get two solutions
sol1 = (-b - math.sqrt(d))/(2*a)
sol2 = (-b + math.sqrt(d))/(2*a)

#print two solutions
print("El valor de x1 es:"), sol1

print("El valor de x2 es:"), sol2
导入数学
从数学导入sqrt
从数学导入pow
#输入值
a=输入('a的值:')
b=输入('b的值:')
c=输入('c的值:')
#计算判别式
d=(b**2)-(4*a*c)
如果d<0:
d*=-1
其他:
d=d
#得到两个解决方案
sol1=(-b-math.sqrt(d))/(2*a)
sol2=(-b+数学sqrt(d))/(2*a)
#打印两个解决方案
打印(“El valor de x1:”),sol1
打印(“El valor de x2 es:”),sol2
结果如下:

Traceback (most recent call last):
  File "/Users/valeriamansilla/PycharmProjects/untitled/Quadratic          Formular.py", line 11, in <module>
    d = (b**2) - (4*a*c)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“/Users/valeriamansilla/PycharmProjects/untitled/Quadratic Formular.py”,第11行,中
d=(b**2)-(4*a*c)
TypeError:不支持**或pow()的操作数类型:'str'和'int'
进程已完成,退出代码为1

input
以字符串形式从用户处获取输入。您需要获取整数值,以便对其执行数学运算:

a = int(input('Value for a: '))
b = int(input('Value for b: '))
c = int(input('Value for c: '))

Python3:
input
返回str而不是整数。转换值就可以了。谢谢,但是如何转换值?@V.Mjo您可以用print(type(a))检查a的类型,然后您将得到“str”而不是“int”。如果您想将str转换为int,只要使用int(a)或float(a)就可以了。