可以更好地利用Python的字符串格式,使用映射整数的函数
n
到
str(n)
如果是肯定的,或者
(-str(n))
如果是阴性的:
def f(n):
return str(n) if n >= 0 else '(%d)' % n
print("The quadratic equation is : {0}x2+{1}x+{2}".format(f(a), f(b), f(c)))
我建议一种更好的格式是实际放置数字的符号,而不是静态的
+
在操作数之间,并避免使用括号:
def f(n):
return ('+' if n >= 0 else '-') + '%d' % abs(n)
eq_f = '{0}x2{1}x{2}'
print("The quadratic equation is : " + eq_f.format(f(a), f(b), f(c)))
输出(示例):
Enter the value of a:-1
Enter the value of b:5
Enter the value of c:-4
The quadratic equation is : -1x2+5x-4