Home > Back-end >  Python formatting float number to one decimal place within a string
Python formatting float number to one decimal place within a string

Time:10-17

I currently have a problem with a program i have to create for my University course.

The purpose of the program is to generate a housing occupancy report. Overall it must display a percentage of houses that have a certain amount of occupants in within a road. enter image description here

However, when i am executing the code i get this outcome.

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

This should also include the the single decimal place. There only be one of each 'Occupant', 'No. Houses' and 'Percentages' row names. As it is displaying two of each.

When i try to input a float point format

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))

It comes out with this error and doesn't display the percentage line.

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'

Down below is my whole code and being advised on what to do would be greatly helpful!

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)

CodePudding user response:

You are printing 10 things, when you have only 9 parameters, including the name of the line. More precisely, you are printing twice parameter 0, which is the name of the line string.

That is why in your result we see twice the name of the line. And, I suspect, since you seems to deal with the second "0" parameter as if it where one of the number, when you tried {0:7.1f}, well, you were trying to print the string "Percentage:" as a float, hence the error.

So, solution is pretty simple. In each of your print remove the second {0:...} part.

For example

    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"))

should be

    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"))

(the first number you want to print is 0 that is the second parameter of format, or parameter 1 of format)

Once you have done that, you still have your percentage problem. But now you have a winning chance. Just retry what you've already tried, and this time it will work. That is just print, for the line percentage the numbers with 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))

which is a copy of what you said that you've tried. I just removed the {0:>7.1f} which means "print parameter 0 as a float with 1 decimal", which makes no sense, since parameter 0 is the string "Percentage:". And I corrected the misplaced % for parameter 7, but that was just a typo, I surmise.

Still not correct that way, because, well, you added a % after each number, so if you want the number, with the %, to occupy 7 characters on the screen, then you need the number, without the %, to occupy 6 characters. So, now replace all those 7.1f by some 6.1f and you are all set.

Or follow one of the comments to see how else you could print percentage (that is replace f by % in the format. Then, you don't need to add the % in between. And then, the size of the number is 7 again. So, all in one ``...{1:>7.1%}{2:>7.1%}...```. But that implies also another simplification: then, no need to compute the percentage (multiply by 100). For example

"{0:>12}{1:>7.1%}".format("Percentage:", 0.25)

is " 25.0%"

But, well, tl;dr: your main problem is the repetition of the printing of parameter 0 in all your formats. Remove the second item of all of them, and you would have found the rest yourself, I am sure.

  • Related