TypeError不受支持的操作数类型

问题描述:

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!" 

工作完全正常,直到我尝试了这些参数。TypeError不受支持的操作数类型

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,5'by 0.5' – Ronald 2014-10-08 11:26:18

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()函数。

+0

认为“可以考虑使用的raw_input()函数”应当认真阅读“** **始终的raw_input使用(),除非你确切地知道你”重新做“... – l4mpi 2014-10-08 11:49:19

+0

@ l4mpi:...或者如果你使用的是Python 3(因为'print'语句,OP必须使用Python 2) – cdarke 2014-10-08 12:03:50

+0

@ l4mpi我同意,但这是引用 – jonrsharpe 2014-10-08 12:13:53