我目前正在用 Python 编写一个程序,它将三项式方程分解为二项式方程。但是,问题是每当我计算二项式时,如果它是正数,那么 + 号将不会出现。例如,当我为 b 输入 2 并为 c 输入 -15 时,我得到了输出
二项式是: (x-3)(x5)
如您所见,第二个二项式没有显示 + 号。我怎样才能解决这个问题?
这是我的代码:
import math
print " This program will find the binomials of an equation."
a = int(raw_input('Enter the first coefficient'))
b = int(raw_input('Enter the second coefficient'))
c = int(raw_input('Enter the third term'))
firstbinomial=str(int((((b*-1)+math.sqrt((b**2)-(4*a*c)))/(2*a))*-1))
secondbinomial=str(int((((b*-1)-math.sqrt((b**2)-(4*a*c)))/(2*a))*-1))
print"The binomials are: (x"+firstbinomial+")(x"+secondbinomial")"
我试过做:
import math
print " This program will find the binomials of an equation."
a = int(raw_input('Enter the first coefficient'))
b = int(raw_input('Enter the second coefficient'))
c = int(raw_input('Enter the third term'))
firstbinomial=str(int((((b*-1)+math.sqrt((b**2)-(4*a*c)))/(2*a))*-1))
if firstbinomial<=0:
sign=""
else:
sign="+"
secondbinomial=str(int((((b*-1)-math.sqrt((b**2)-(4*a*c)))/(2*a))*-1))
if secondbinomial<=0:
sign=""
else:
sign="+"
print"The binomials are: (x"+sign+firstbinomial+")(x"+sign+secondbinomial")"
但是我最终得到:
二项式是: (x+-3)(x+5)