Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何格式化二次方程输出?_Python_Python 3.x_Complex Numbers - Fatal编程技术网

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

Python 如何格式化二次方程输出?,python,python-3.x,complex-numbers,Python,Python 3.x,Complex Numbers,我的程序中的所有东西都能工作,除了打印出来的时候。解决方案是:s1和s2打印出来,答案后加上+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: &

我的程序中的所有东西都能工作,除了打印出来的时候。解决方案是:s1和s2打印出来,答案后加上+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"))
只需打印出您的复数部分:

将相关行更改为

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


我修改了代码,看看这是否稍微好一点:

导入cmath 输入数学 将输入转换为浮点以避免ValueError a=0 当a==0时: 尝试: a=浮点输入以下各项的值: 如果a==0: 调零误差 除零误差外: 打印您输入的值无效。不允许为零 除值错误外: 打印您输入的值无效。只有实数 其他: 打破 打印 尽管如此: 尝试: b=浮动输入b的值: 除值错误外: 打印您输入的值无效。只有实数 其他: 打破 打印 尽管如此: 尝试: c=浮点输入c的值: 除值错误外: 打印您输入的值无效。只有实数 其他: 打破 打印 计算判别式,打印并格式化 圆盘=b**2-4*a*c 打印判别式等于:,圆盘 打印 计算/打印解决方案(如果有一个、两个或没有) 如果圆盘<0: 打印没有真正的解决方案 elif disc==0: x=-b+cmath.sqrtdisc/2*a x=-b/2*a 打印解决方案是,x 其他: s1=-b+cmath.sqrtdisc/2*a s2=-b-cmath.sqrtdisc/2*a s1=-b+math.sqrtdisc/2*a s2=-b-math.sqrtdisc/2*a 打印解决方案为+formats1、.2f和+formats2、.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

你有一个打印解决方案是,没有格式的x。你指的是这个吗?0的平方根是0j。因此,你可能最终会这样做。我否决了你,因为OP可能想使用cmath而不是数学。解决方案应该解决打印问题,而不是计算问题。请修正一下,这样我就可以投票了@JoeFerndz是真的,但是jason wong在这里有一个观点。不需要使用cmath,因为当判别数小于0时,代码会失效。@jason wong,更新代码而不解释原因对操作没有帮助。下次请澄清选择该选项的原因。要删除否决票,您需要编辑答案。@Joe Ferndz我基本上只是用数学替换cmath。使用cmath或math实际上取决于最终目标。但你说得有道理,下次我应该解释一下。
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