我目前有一個必須為我的大學課程創建的程式的問題。
該程式的目的是生成住房占用報告。總體而言,它必須顯示在道路內有一定數量居住者的房屋的百分比。 在此處輸入影像描述
但是,當我執行代碼時,我得到了這個結果。
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)
uj5u.com熱心網友回復:
當您只有 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,或 的引數 1 format)
一旦你這樣做了,你仍然有你的百分比問題。但現在你有一個獲勝的機會。只需重試您已經嘗試過的方法,這一次它會起作用。那只是列印,對于percentage帶有 format 的數字的行{:>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://www.uj5u.com/qiye/516505.html
上一篇:從輸入中翻譯Unicode字符
