product_dict = {"Jeans": 150, "Jacket": 300, "Perfume": 50, "Wallet": 125, "Glasses": 100}
#product dictonary with prices
order_history = {'Andrew':[{'Jeans':2, 'Wallet': 4}, {'Perfume':2}], 'Max':[{'Jacket': 3}]}
c_name = input('customer name: ')
print('The following is order history of, c_name')
key = order_history['c_name']
for i in range(len(key)):
print('purchase', i 1, key[i])
I am creating a retail check out program, I have a predefined products stored in a dictionary along with prices that I use in other part of program, I wanted to print the output in a more presentable like this:
This is the order history of Andrew.
Jeans Wallet Perfume Jacket
Purchase 1: 2 4
Purchase 2: 2
CodePudding user response:
This is more of a console display issue rather than a python issue.
You could potentially use a library for this, such as Textualize/rich. I don't want to give you the full answer, just lead you in the right direction as it appears you are learning:
from rich.console import Console
from rich.table import Table
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Jeans", style="dim", width=12)
table.add_column("Jacket")
table.add_column("Perfume", justify="right")
table.add_column("Wallet", justify="right")
#add more columns as necessary
#iterate over your order history to add rows
table.add_row(
)
console.print(table)
In my opinion, creating a table in the console would look best.
CodePudding user response:
Use a nested loop to print the quantity of each item, by looping over the keys of product_dict
. Use end=''
to print all the quantities on the same line, and use fixed-width fields in the formatting string to make everything line up.
product_dict = {"Jeans": 150, "Jacket": 300, "Perfume": 50, "Wallet": 125, "Glasses": 100}
#product dictonary with prices
order_history = {'Andrew':[{'Jeans':2, 'Wallet': 4}, {'Perfume':2}], 'Max':[{'Jacket': 3}]}
c_name = 'Andrew'
print(f'The following is order history of {c_name}')
print(' ' * 12, end='')
for product in product_dict:
print(f'{product:10}', end='')
print()
key = order_history[c_name]
for i, items in enumerate(key, 1):
print(f'Purchase {i}: ', end='')
for product in product_dict:
if product in items:
print(f'{items[product]:<10}', end='')
else:
print(' ' * 10, end='')
print()
CodePudding user response:
If you want to accommodate additions to your dictionary with arbitrary length product names:
product_dict = {"Jeans": 150, "Jacket": 300, "Perfume": 50, "Wallet": 125, "Glasses": 100}
skus = list(product_dict)
width = 0
for sku in skus:
l = len(sku)
if l > width:
width = l
width = width 2
pl = len('Purchase XX')
h = ' '.center(pl 1,' ')
for sku in skus:
h = h sku.center(width,' ')
#product dictonary with prices
order_history = {'Andrew':[{'Jeans':2, 'Wallet': 4}, {'Perfume':2}], 'Max':[{'Jacket': 3}]}
c_name = input('customer name: ')
print('The following is order history of', c_name)
print(h)
key = order_history[c_name]
for i in range(len(key)):
s = ''
for sku in skus:
if sku in list(key[i]):
s = s str(key[i][sku]).center(width,' ')
else:
s = s ' '.center(width,' ')
print('Purchase ' str(i 1).rjust(2,' ') ' ' s)
You'll get something like this:
The following is order history of Andrew
Jeans Jacket Perfume Wallet Glasses
Purchase 1 2 4
Purchase 2 2