Advertisement
IMustRemainUnknown

Python Dictionary Challenge

Dec 11th, 2023
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | Source Code | 0 0
  1. transactions = [
  2.     {'id': 1, 'product': 'apple', 'quantity': 3, 'price': 1.25},
  3.     {'id': 2, 'product': 'banana', 'quantity': 2, 'price': 0.75},
  4.     {'id': 3, 'product': 'apple', 'quantity': 1, 'price': 1.25},
  5.     {'id': 4, 'product': 'orange', 'quantity': 4, 'price': 1},
  6.     {'id': 5, 'product': 'banana', 'quantity': 2, 'price': 0.75},
  7.     {'id': 6, 'product': 'orange', 'quantity': 1, 'price': 1},
  8.     {'id': 7, 'product': 'orange', 'quantity': 2, 'price': 1},
  9.     {'id': 8, 'product': 'orange', 'quantity': 3, 'price': 1},
  10.     {'id': 9, 'product': 'dalandan', 'quantity': 3, 'price': 3},
  11.     {'id': 10, 'product': 'dalandan', 'quantity': 2, 'price': 3},
  12. ]
  13.  
  14.  
  15. def generate_report(transactions):
  16.     product_revenue = dict()
  17.     total = 0
  18.  
  19.     for i in range(len(transactions)):
  20.         quantity = transactions[i][list(transactions[i])[2]]
  21.         price = transactions[i][list(transactions[i])[3]]
  22.         product = transactions[i][list(transactions[i])[1]]
  23.  
  24.         if product in product_revenue:
  25.             product_revenue[product] = product_revenue[product] + quantity * price
  26.         else:
  27.             product_revenue[product] = quantity * price
  28.  
  29.         total = total + (quantity * price)
  30.  
  31.     for i, j in product_revenue.items():
  32.         print(f"{i} : {j}")
  33.  
  34.     print("Total :", total)
  35.  
  36.  
  37. generate_report(transactions)
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement