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