-1

for our python class we are required to have the formatting be perfect to the example given. The example looks like this:

   Average       Gallons       Cost Per   
    Speed          Used         Gallon    
    65.62         72.41          2.07     

My output looks like this:

Average       Gallons       Cost Per      
Speed         Used          Gallon        
65.62         72.41          2.07          

I need to know how to be able to format my output to look like the one given in the example. Help would be heavily appreciated. Here is my code:

driven = 1050
mpg = 14.5
money = input("How much money was spent: ")
hours = input("Hours driven: ")
average = driven / hours
num = driven / mpg
cpg = money / num
print("{:<14}{:<14}{:<14}".format("Average", "Gallons", "Cost Per"))
print("{:<14}{:<14}{:<14}".format("Speed", "Used", "Gallon"))
print("{:<14.2f}{:<14.2f} {:<14.2f}".format(average, num, cpg))

Thanks in advance!

NukeSnicks
  • 29
  • 6
  • Reduce your script to the values that have to be formatted. Thanks! – Wolf Jun 11 '21 at 17:57
  • Why not ask the [Python docs](https://docs.python.org/3.9/library/string.html#formatspec) first? – Wolf Jun 11 '21 at 18:01
  • Does this answer your question? [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) – user_na Jun 11 '21 at 18:05

1 Answers1

0

Using ^ instead of < should do the trick:

driven = 1050
mpg = 14.5
money = int(input("How much money was spent: "))
hours = int(input("Hours driven: "))
average = driven / hours
num = driven / mpg
cpg = money / num
print("{:^14}{:^14}{:^14}".format("Average", "Gallons", "Cost Per"))
print("{:^14}{:^14}{:^14}".format("Speed", "Used", "Gallon"))
print("{:^14.2f}{:^14.2f} {:^14.2f}".format(average, num, cpg))

Output:

   Average       Gallons       Cost Per    
    Speed          Used         Gallon     
    52.50         72.41          27.62     
Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39