From ec977fe155c22d55a37779b8ae9e58262850ee8c Mon Sep 17 00:00:00 2001 From: Christoffer Viken Date: Fri, 10 Feb 2017 13:56:34 +0000 Subject: [PATCH] Product/user summary now ordered by number of products bought --- text_based.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/text_based.py b/text_based.py index f0752d1..d644a5f 100755 --- a/text_based.py +++ b/text_based.py @@ -825,30 +825,19 @@ class ShowUserMenu(Menu): less(string) def print_purchased_products(self, user): - products = {} + products = [] for ref in user.products: product = ref.product - if product in products: - products[product] += ref.count - else: - products[product] = ref.count - #for transaction in user.transactions: - # if transaction.purchase: - # for entry in transaction.purchase.entries: - # n = entry.product.name - # if n in products: - # products[n]+=1 - # else: - # products[n]=1 + count = ref.count + products.append((product, count)) num_products = len(products) if num_products == 0: - print 'No products purchased yet' + print('No products purchased yet') else: text = '' text += 'Products purchased:\n' - for product in products: - text += ('%-'+str(Product.name_length)+'s %3i\n') \ - % (product, products[product]) + for product, count in products: + text += '{0:<47} {1:>3}\n'.format(product.name, count) less(text)