Dom*_*aft 0 python binomial-coefficients python-2.7
二项式是:(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")"
Run Code Online (Sandbox Code Playgroud)
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")"
Run Code Online (Sandbox Code Playgroud)
二项式是:(x + -3)(x + 5)
您需要使用字符串格式来显示正号,或者+在字符串中明确使用:
firstbinomial = (((b * -1) + math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)) * -1
secondbinomial = (((b * -1) - math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)) * -1
print "The binomials are: (x{:+.0f})(x{:+.0f})".format(firstbinomial, secondbinomial)
# prints "The binomials are: (x-3)(x+5)"
Run Code Online (Sandbox Code Playgroud)
(将值保留为浮点数但格式不带小数点),或者
print "The binomials are: (x+{})(x+{})".format(firstbinomial, secondbinomial)
# prints "The binomials are: (x+-3)(x+5)"
Run Code Online (Sandbox Code Playgroud)
将-只显示因为负值总是印有其标志.
| 归档时间: |
|
| 查看次数: |
1734 次 |
| 最近记录: |