我现在遇到了一个问题,我必须为我的大学课程创建一个程序。
该方案的目的是生成一份住房占用报告。总的来说,它必须显示一定比例的房屋,有一定数量的居住者在一条道路上。在这里输入图像描述
但是,当我执行代码时,我得到了这个结果。
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
这也应该包括小数点一位。每个“占用者”、“没有房子”和“百分比”行名中只有一个。因为它显示了两个人。
当我尝试输入浮点格式时
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)
发布于 2022-10-15 23:20:49
当您只有9个参数时,您正在打印10件东西,包括该行的名称。更准确地说,您要打印两次参数0,即行字符串的名称。
这就是为什么在您的结果中,我们看到了一行名的两倍。而且,我怀疑,因为您似乎处理了第二个"0“参数,就好像它处理其中的一个数字,当您尝试{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"))
(要打印的第一个数字是0
,它是format
的第二个参数,或format
的参数1)
一旦你这样做了,你仍然有你的百分比问题。但现在你有了获胜的机会。只要重试你已经尝试过的东西,这一次它就会成功。这只是打印,对于行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:>7.1f}
,意思是“打印参数0作为一个带有1小数点的浮点数”,这是没有意义的,因为参数0是字符串“百分比:”。我修正了参数7的错误的%
,但我推测这只是一个错误。
但这种方法仍然不正确,因为您在每个数字之后添加了一个%,所以如果您希望这个数字与%一起在屏幕上占据7个字符,那么您需要这个数字,而不是%,以占用6个字符。所以,现在用一些7.1f
替换所有的6.1f
,您就都设置好了。
或者按照其中一个注释查看如何打印百分比(即用格式的f
替换%
)。然后,您不需要在中间添加%
。然后,这个数字的大小又是7。所以,在一个...{1:>7.1%}{2:>7.1%}...`.中但这也意味着另一个简化:然后,不需要计算百分比(乘以100)。例如
"{0:>12}{1:>7.1%}".format("Percentage:", 0.25)
是“25.0%”
但是,嗯,tl;dr:您的主要问题是重复以所有格式打印参数0。把它们中的第二项去掉,我敢肯定,你自己也会找到剩下的。
https://stackoverflow.com/questions/74081364
复制相似问题