0

I recently saw a post on how to align outputs (Python: Format output string, right alignment). The problem about the answers on that page is that none of the responses answered my question. Yes, it taught me how to align single outputs but not outputs of a different length. This is an example code to show you what I mean:

purchasableItems = {'1234' : {'name' : 'tomato' : 'price' : 0.20}
                    '8765' : {'name' : 'mango' : 'price' : 1},
                    '5678' : {'name' : 'pineapple' : 'price' : 1}}
choice = input("Code: ") #write the items 4 digit code
amount = input("Amount: ") #how much of that item
price = int(amount) * (purchasableItems[choice]['price']
print("That would be {} {} {:>5} {:>5} {:>5}".format(choice, purchasableItems[choice]['name'], amount, purchasableItems[choice]['price'], price))

I have tried to re arrange the '{:>5}' into various other numbers but it didn't allow me to print into the way I wanted, I would like it to print out as:

Code: 1234
Amount: 3
That would be: 1234 tomato    3   0.20    0.60
Code: 8765
Amount: 2
That would be: 8765 mango     2   1.00    2.00

When the user is done I want the program to output a receipt:

Your receipt is:
1234 tomato   3   0.20   0.60
8765 mango    2   1.00   2.00

As you can see the receipt is aligned perfectly.

But it actually outputs as, depending on the length of the item

Your receipt is:
1234 tomato   3   0.20   0.60
5678 pineapple    2   1.00   2.00

For the love of anything I can't figure it out

Community
  • 1
  • 1
  • See if this answer helps: http://stackoverflow.com/questions/40321732/python-print-list-of-csv-strings-in-aligned-columns/40321855#40321855 – Alex Hall Oct 30 '16 at 13:25
  • Sorry for the late reply but unfortunately that did not answer my question – FlagShipKILLER Oct 30 '16 at 20:13
  • What happens when you try to adapt the code there to your purposes? – Alex Hall Oct 30 '16 at 20:37
  • It out puts in this fashion: `Your receipt is: 1234 tomato 3 0.20 0.60 8765 mango 2 1.00 2.00' unaligned. The difference is much more clear to see in examples such as: `Your receipt is: 1234 tomato 3 0.20 0.60 8765 pineapple 2 1.00 2.00` – FlagShipKILLER Oct 30 '16 at 21:06
  • unfortunately, it doesn't quite show up correctly in comments so I edited my post – FlagShipKILLER Oct 30 '16 at 21:07
  • I'm asking, what happens when you try to adapt the code in the answer that I linked to your purposes? – Alex Hall Oct 30 '16 at 21:24

1 Answers1

0

You can use \t enter a tab break to line prints up. If it hasn't quite lined up you can have more than one. \t\t

I'm having trouble demonstrating this with your code as I am getting syntax errors. But here is an example from one of my scripts:

print ("Floating Value:\t" , value)
print ("Decimal Value:\t" , finalans)

This is output:

Floating Value:  010000111111101101
Decimal Value:   123.25

Another example with multiple \ts:

print ("Diameter = \t\t" , 2 * radius , "cm")
print ("Circumference = \t" , 2 * radius * pi , "cm")
print ("Area = \t\t\t" , pi * radius ** 2 , "cm")
August Williams
  • 907
  • 4
  • 20
  • 37