如果你想让它更具可读性,这是另一种选择:
def beauty(coeff, i):
if(coeff == 0): return ''
if(i == 2):
if(coeff == 1): return "x\u00B2"
if(coeff == -1): return "-x\u00B2"
return f"{coeff}x\u00B2"
if(i == 1):
if(coeff == 1): return "+x"
if(coeff == -1): return "-x"
if(coeff > 0): return f"+{coeff}x"
return f"{coeff}x"
if(i == 0):
if(coeff > 0): return f"+{coeff}"
return f"{coeff}"
def PrintQuadratic():
a = int(input('a: '))
b = int(input('b: '))
c = int(input('c: '))
print(f"{beauty(a,2)}{beauty(b,1)}{beauty(c,0)}")
PrintQuadratic()
a: 7
b: 9
c: 13
â 7x²+9x+13
PrintQuadratic()
a: -1
b: 1
c: 0
â -x²+x
PrintQuadratic()
a: 4
b: -2
c: 1
â 4x²-2x+1
它有点长,但能产生很好的指纹。