提问人:DarkPhobos 提问时间:10/16/2022 更新时间:10/16/2022 访问量:214
Python 将浮点数格式化为字符串中的小数点后一位
Python formatting float number to one decimal place within a string
问:
我目前对必须为我的大学课程创建的程序有疑问。
该程序的目的是生成住房占用报告。总体而言,它必须显示道路内有一定数量居住者的房屋的百分比。在此处输入图像描述
但是,当我执行代码时,我得到了这个结果。
Occupants: Occupants: 0 1 2 3 4 5 6 6+
No. Houses: No. Houses: 1 2 3 4 5 6 7 8
Percentage: Percentage: 2.77777777777777775.5555555555555558.33333333333333411.1111111111111113.8888888888888916.66666666666666819.44444444444444322.22222222222222
这还应包括小数点后一位。每个“居住者”只有一个,“不。Houses“和”Percentages“行名称。因为它显示每个两个。
当我尝试输入浮点格式时
print("{0:>12}{0:>7.1f}%{1:>7.1f}%{2:>7.1f}%{3:>7.1f}%{4:>7.1f}%{5:>7.1f}%{6:>7.1f}%{7:>7%.1f}{8:>7.1f}%".format("Percentage: ",p0, p1, p2, p3, p4, p5, p6, p7))
它出现此错误,并且不显示百分比线。
Occupants: Occupants: 0 1 2 3 4 5 6 6+
No. Houses: No. Houses: 10 20 30 40 50 60 70 8
Unknown format code 'f' for object of type 'str'
下面是我的整个代码,被告知该怎么做会很有帮助!
def get_data():
h0 = int(input("Provide the number of houses with 0 occupants: "))
h1 = int(input("Provide the number of houses with 1 occupants: "))
h2 = int(input("Provide the number of houses with 2 occupants: "))
h3 = int(input("Provide the number of houses with 3 occupants: "))
h4 = int(input("Provide the number of houses with 4 occupants: "))
h5 = int(input("Provide the number of houses with 5 occupants: "))
h6 = int(input("Provide the number of houses with 6 occupants: "))
h7 = int(input("Provide the number of houses with 6+ occupants: "))
return h0, h1, h2, h3, h4, h5, h6, h7
def percentage(h0, h1, h2, h3, h4, h5, h6, h7):
total = (h0 + h1 + h2 + h3 + h4 + h5 + h6 + h7)
return h0*100/total, h1*100/total, h2*100/total, h3*100/total, h4*100/total, h5*100/total, h6*100/total, h7*100/total
def display_results(h0, h1, h2, h3, h4, h5, h6, h7, p0, p1, p2, p3, p4, p5, p6, p7):
print("Results")
print("{0:>12}{0:>7}{1:>7}{2:>7}{3:>7}{4:>7}{5:>7}{6:>7}{7:>7}{8:>7}".format("Occupants:", 0, 1, 2, 3 ,4 ,5 ,6, ">6"))
print("{0:>12}{0:>7}{1:>7}{2:>7}{3:>7}{4:>7}{5:>7}{6:>7}{7:>7}{8:>7}".format("No. Houses:", h0, h1, h2, h3, h4, h5, h6, h7))
print("{0:>12}{0:>7}{1:>7}{2:>7}{3:>7}{4:>7}{5:>7}{6:>7}{7:>7}{8:>7}".format("Percentage: ",p0, p1, p2, p3, p4, p5, p6, p7))
if __name__== "__main__":
h0, h1, h2, h3, h4, h5, h6, h7 = get_data()
p0, p1, p2, p3, p4, p5, p6, p7 = percentage (h0, h1, h2, h3, h4, h5, h6, h7)
display_results (h0, h1, h2, h3, h4, h5, h6, h7, p0, p1, p2, p3, p4, p5, p6, p7)
答:
当您只有 9 个参数(包括行的名称)时,您正在打印 10 个内容。 更准确地说,您打印了两次参数 0,即行字符串的名称。
这就是为什么在您的结果中,我们看到两倍的行名。而且,我怀疑,由于您似乎将第二个“0”参数处理为其中一个数字,因此当您尝试时,您正在尝试将字符串“Percentage:”打印为浮点数,因此出现错误。{0:7.1f}
所以,解决方案很简单。在每个中删除第二部分。print
{0:...}
例如
print("{0:>12}{0:>7}{1:>7}{2:>7}{3:>7}{4:>7}{5:>7}{6:>7}{7:>7}{8:>7}".format("Occupants:", 0, 1, 2, 3 ,4 ,5 ,6, ">6"))
应该是
print("{0:>12}{1:>7}{2:>7}{3:>7}{4:>7}{5:>7}{6:>7}{7:>7}{8:>7}".format("Occupants:", 0, 1, 2, 3 ,4 ,5 ,6, ">6"))
(要打印的第一个数字是 的第二个参数,或参数 10
format
format
)
一旦你这样做了,你仍然有你的百分比问题。但现在你有了一个获胜的机会。只需重试您已经尝试过的操作,这次它就会起作用。那只是打印,对于带有格式的数字行.percentage
{:>7.1f}
print("{0:>12}{1:>7.1f}%{2:>7.1f}%{3:>7.1f}%{4:>7.1f}%{5:>7.1f}%{6:>7.1f}%{7:>7.1f}%{8:>7.1f}%".format("Percentage: ",p0, p1, p2, p3, p4, p5, p6, p7))
这是你说的你已经尝试过的副本。我刚刚删除了表示“将参数 0 打印为小数点后 1 的浮点数”,这毫无意义,因为参数 0 是字符串“Percentage:”。
我纠正了参数 7 的错位,但我推测这只是一个错别字。{0:>7.1f}
%
这种方式仍然不正确,因为,好吧,你在每个数字后面加了一个 %,所以如果你想让数字,加上 %,在屏幕上占据 7 个字符,那么你需要这个数字,没有 %, 占据 6 个字符。所以,现在用一些替换所有这些,你就准备好了。7.1f
6.1f
或者按照其中一条注释查看如何打印百分比(即在格式中替换为。然后,您无需在两者之间添加然后,数字的大小又是 7。所以,多合一“......{1:>7.1%}{2:>7.1%}...'''。但这也意味着另一种简化:然后,无需计算百分比(乘以 100)。
例如f
%
%
"{0:>12}{1:>7.1%}".format("Percentage:", 0.25)
是“ 25.0%”
但是,好吧,tl;DR:您的主要问题是在所有格式中重复打印参数 0。我敢肯定,删除所有这些中的第二项,其余的你都会自己找到。
评论