您好我是python的新手,我正在尝试制作一个执行二次方程式的程序但由于某种原因它不会将两个变量一起添加
#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 :(得分:2)
您正在计算负值的sqrt。结果不是真实的,而是虚数。
因此,所有以下计算都是以复数形式完成的。当你看到-3 + 5.196152422706632j这样的结果时,这意味着由真实(-3)和虚部组成的复数,由j
表示,等于5.196152422706632。