如何格式化二次方程输出?

how do I format quadratic equation output?

我的程序中的所有内容都可以正常工作,除非它打印出“解决方案是:” s1s2 在答案后打印出 +0.00j。如何将输出格式化为两位小数?如您所见,我尝试了 ,.2f 但它没有用,我们将不胜感激。

import cmath

#converting inputs into floats to avoid ValueError 
a = 0
while a == 0:
    try:
        a = float(input("Enter a value for a: "))
        if a == 0:
           raise ZeroDivisionError
    except ZeroDivisionError:
        print("The value you entered is invalid. Zero is not allowed")
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

while True:
    try:
        b = float(input("Enter a value for b: "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

while True:
    try:
        c = float(input("Enter a value for c: "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

#Calcualting discriminant, printing it and formatting it    

disc = (b**2) - (4*a*c)
print("The discriminant is equal to: " , disc)
print()

#Calucating/printing solutions if there is one, two, or none
if disc < 0:
    print ("No real solution")
elif disc == 0:
    x = (-b+cmath.sqrt(disc))/(2*a)
    print ("The solution is " , x)
else:
    s1 = (-b+cmath.sqrt(disc))/(2*a)
    s2 = (-b-cmath.sqrt(disc))/(2*a)
    print ("The solutions are " + format(s1,",.2f"), "and " + format(s2, ",.2f"))

只需打印出复数的 .real 部分:

将相关行改为

print(f"The solution is {x.real:.2f}")

print (f"The solutions are {s1.real:.2f} and {s2.real:.2f}")

我修改了代码看看是不是稍微好一点:

# import cmath
import math

#converting inputs into floats to avoid ValueError 
a = 0
while a == 0:
    try:
        a = float(input("Enter a value for a: "))
        if a == 0:
           raise ZeroDivisionError
    except ZeroDivisionError:
        print("The value you entered is invalid. Zero is not allowed")
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
        break
print()

while True:
    try:
        b = float(input("Enter a value for b: "))
    except ValueError:
        print("The value you entered is invalid. only real numbers")
    else:
        break
print()

while True:
    try:
        c = float(input("Enter a value for c: "))
    except ValueError:
        print("The value you entered is invalid. only real numbers")
    else:
        break
print()

#Calcualting discriminant, printing it and formatting it    

disc = (b**2) - (4*a*c)
print("The discriminant is equal to: " , disc)
print()

#Calucating/printing solutions if there is one, two, or none
if disc < 0:
    print ("No real solution")
elif disc == 0:
    # x = (-b+cmath.sqrt(disc))/(2*a)
    x = -b/(2*a)
    print ("The solution is " , x)
else:
    #s1 = (-b+cmath.sqrt(disc))/(2*a)
    #s2 = (-b-cmath.sqrt(disc))/(2*a)
    s1 = (-b+math.sqrt(disc))/(2*a)
    s2 = (-b-math.sqrt(disc))/(2*a)
    print ("The solutions are " + format(s1,",.2f"), "and " + format(s2, ",.2f"))

结果:

Enter a value for a: 1

Enter a value for b: 5

Enter a value for c: 4

The discriminant is equal to:  9.0

The solutions are -1.00 and -4.00