2014-10-08 44 views
-1
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

回答

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

+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

相關問題