Advertisement
pavlinpetkov420

Untitled

Jan 9th, 2021
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def calculate_total_n_print(fpq_dict):
  5. # iterate through the dictionary add output as it is needed to be
  6. # calculate total price and add it at the end of the string
  7. total_price = 0
  8. output = "Bought furniture:\n"
  9. for name, price in fpq_dict.items():
  10. output += f"{name}\n"
  11. total_price += price
  12. output += f"Total money spend: {total_price:.2f}"
  13. print(output)
  14.  
  15.  
  16. def extract_data(cmd, fpq_dict):
  17. # if there is a match, store it in dictionary {'name': total_price_of_product}
  18. pattern = r">>(?P<name>\w+)<<(?P<price>\d+|\d+(.\d+))!(?P<quantity>\d+)"
  19. matches = re.finditer(pattern, cmd)
  20. if matches:
  21. name, total_price = "", 0.0
  22. for el in matches:
  23. name = el.group('name')
  24. total_price = float(el.group('price')) * int(el.group('quantity'))
  25. if name != '':
  26. fpq_dict[name] = total_price
  27. return fpq_dict
  28.  
  29.  
  30. def gather_data():
  31. # fpq - furniture price quantity dict
  32. fpq_dict = {}
  33.  
  34. while True:
  35. cmd = input()
  36. if cmd == "Purchase":
  37. calculate_total_n_print(fpq_dict)
  38. break
  39. fpq_dict = extract_data(cmd, fpq_dict)
  40.  
  41.  
  42. gather_data()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement