小编new*_*ntu的帖子

在 Python3 中打印格式化的浮点列表

我正在编写一个执行以下操作的程序:

  1. 采用逗号分隔的多个输入(浮点)
  2. 对列表的所有元素执行计算
  3. 输出一个列表(带两位小数的浮点数)

我编写了以下程序

import math

C = 50.0
H = 30.0
Q = []

D = input("Please input the D-Values\n").split(",")
[float(k) for k in D]
#[int(k) for k in D]

print("D = {}".format(D))

for i in D:
    j = (math.sqrt((2*C*float(i))/H))
    Q.append(j) 


print("The answers are")
print(Q)
print(type(Q[0]))
print("Q = {:.2f}".format(Q))
Run Code Online (Sandbox Code Playgroud)

我在执行这个程序时遇到以下错误

Traceback (most recent call last):
  File "/home/abrar/pythonPrograms/Challange6.py", line 24, in <module>
    print("Q = {:.2f}".format(Q))
TypeError: non-empty format string passed to object.__format__
Run Code Online (Sandbox Code Playgroud)

我试图寻找这个问题的解决方案,但找不到答案。如果不包含 {:.2f},即使用 {},程序运行良好。但是,输出看起来非常混乱。

任何帮助都受到高度赞赏。

python printing floating-point formatting python-3.x

3
推荐指数
1
解决办法
4664
查看次数