当前位置: 首页 > 知识库问答 >
问题:

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

夹谷野
2023-03-14

我的程序中的所有内容都正常工作,除了打印出“解决方案是:”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"))

共有2个答案

景震博
2023-03-14

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

# 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
蔚学林
2023-03-14

只需打印出。实数复数的一部分:

将相关行更改为

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

print (f"The solutions are {s1.real:.2f} and {s2.real:.2f}")
 类似资料:
  • 我们可以看到格式化就是通过格式字符串得到特定格式: format!("{}", foo) -> "3735928559" format!("0x{:X}", foo) -> "0xDEADBEEF" format!("0o{:o}", foo) -> "0o33653337357" 根据使用的参数类型,同样的变量(foo)能够格式化成不同的形式:X, o 和未指定形式。 这个格式化的功能是通过 t

  • 前面章节介绍了如何使用 % 操作符对各种类型的数据进行格式化输出,这是早期 Python 提供的方法。自 Python 2.6 版本开始,字符串类型(str)提供了 format() 方法对字符串进行格式化,本节就来学习此方法。 format() 方法的语法格式如下: str.format(args) 此方法中,str 用于指定字符串的显示样式;args 用于指定要进行格式转换的项,如果有多项,之

  • 主要内容:实例,实例,格式变量,实例,实例,实例,实例,输出到其它文件,实例,实例Perl 是一个非常强大的文本数据处理语言。 Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据。 Perl 格式化定义语法格式如下: 参数解析: FormatName :格式化名称。 fieldline :一个格式行,用来定义一个输出行的格式,类似 @,^,<,>,| 这样的字符。 value_one,value_two…… :数据行,用来向前面的格式行

  • 主要内容:实例,运行实例标签呈现HTML文本,但可以接受参数化输入。 以下JSF代码 - 被渲染成以下HTML代码 - 实例 打开NetBean8.2,创建一个名称为:OutputFormat 的JavaWeb JSF工程。以下是文件:UserBean.java中的代码 - 以下是文件:index.xhtml中的代码 - 运行实例 Tomcat启动完成后,在浏览器地址栏中输入以下URL。 得到结果如下图所示 -

  • 本文向大家介绍JSON格式化输出,包括了JSON格式化输出的使用技巧和注意事项,需要的朋友参考一下 今天有个需求是对输出的JSON进行格式化 首先想到的就是jsBeautifier之类的小插件 搜索了一番看到有一位朋友回答JSON.stringify可以输出格式化的JSON字符串 唔,不错 PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用: 在线JSON代码检验

  • 2.6.3 格式化输出 很多应用都要求将数据按整齐的格式输出,用 print 语句能够安排简单的格式。例如, 下面的程序画出一棵简单的圣诞树: 【程序 2.4】eg2_4.py print " * " print " **@ " print " *@*** " print " *****@* " print "*********" print " *