TypeError不支持的操作数类型

时间:2014-10-08 11:24:10

标签: python python-2.7 math

from math import *

a = input("A= ")
b = input("B= ")
c = input("C= ")

d = b*b-(4*a*c)

print "The discriminent is: %s!" %d
if d >= 0:
    x1 = (0-b+sqrt(d))/2*a
    x2 = (0-b-sqrt(d))/2*a
    print "First answer is: %s!" %x1
    print "Second answer is: %s!" %x2
else:
    print "X can't be resolved!"

在我尝试这些参数之前一直工作正常。

A= 0,5
B= -2
C= 2

然后打印出这个

Traceback (most recent call last):
  File "C:/Users/Mathias/Documents/Projects/Skole/project/test/Math.py", line 9, in <module>
    d = b*b-(4*a*c)
TypeError: unsupported operand type(s) for -: 'int' and 'tuple'

我似乎无法弄清楚如何解决这个问题,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

Python使用句点.表示小数点,表示逗号,;您的输入应为0.5

如果你使用了推荐的话,这会给你一个错误:

a = float(raw_input("A= "))

而不是input(相当于eval(raw_input())并且0,5解释为两元组(0, 5) ):

>>> eval("0,5")
(0, 5)
>>> float("0,5")

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    float("0,5")
ValueError: invalid literal for float(): 0,5

the documentation for input

  

考虑将raw_input()函数用于用户的一般输入。

相关问题