2017-03-02 36 views
-2

你好,我是新來的蟒蛇,我試圖讓一個程序,做了二次公式,但由於某種原因,它不會增加兩個變量共同Python的 - 兩個variabels不加在一起

#The Quadratic Formula Calculator 
from cmath import sqrt 

a = (int(input('Enter the value of A : '))) 
b = (int(input('Enter the value of B : '))) 
c = (int(input('Enter the value of C : '))) 

def state_formula(): 
print('Calculating') 

btimesb = b * b 
print('First step is done, the value is', btimesb) 

fourac = -4 * a * c 
print ('The second step is done, the value is', fourac) 

squareit = sqrt(btimesb + fourac) 
print('The third step is done, the value is', squareit) 

negativeb = -b 
print('The fourth step is done, the value is', negativeb) 

addthemplus = negativeb + squareit 
print('The fith step is done, the value is', addthemplus) 

和我它出來作爲

Enter the value of A : 3 
Enter the value of B : 3 
Enter the value of C : 3 
First step is done, the value is 9 
The second step is done, the value is -36 
The third step is done, the value is 5.196152422706632j 
The fourth step is done, the value is -3 
The fith step is done, the value is (-3+5.196152422706632j) 

我想知道,爲什麼第五步實際上並沒有將兩者結合起來,我試圖將其轉換爲浮動,並沒有讓我。

+0

你是什麼意思,「實際上沒有把兩者加在一起」?看起來加入了我。這不像有一些簡單的方法來編寫-3 + 5.196152422706632j。 – user2357112

+0

好的謝謝,不知道是不是正確 –

+0

附加的'j'表示它是一個複數的虛數部分。一個實數「-3」加一個複數「5.196152422706632j」只是另一個複數(這次是實數部分):'-3 + 5.196152422706632j' – MSeifert

回答

2

您正在計算負值的sqrt。結果不是真實的,而是虛數。

結果所有以下計算都是以複數完成的。當看到-3 + 5.196152422706632j等結果時,表示由實數(-3)和虛數部分組成的複數,如j所示,等於5.196152422706632。