SimeonTs

SUPyF2 Dict-Exercise - 04. Orders

Oct 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#3
  4.  
  5. SUPyF2 Dict-Exercise - 04. Orders
  6.  
  7. Problem:
  8. Write a program that keeps information about products and their prices.
  9. Each product has a name, a price and a quantity. If the product doesn't exist yet, add it with its starting quantity.
  10. If you receive a product, which already exists,
  11. increase its quantity by the input quantity and if its price is different, replace the price as well.
  12. You will receive products' names, prices and quantities on new lines.
  13. Until you receive the command "buy", keep adding items.
  14. When you do receive the command "buy",
  15. print the items with their names and total price of all the products with that name.
  16. Input
  17. • Until you receive "buy", the products will be coming in the format: "{name} {price} {quantity}".
  18. • The product data is always delimited by a single space.
  19. Output
  20. • Print information about each product in the following format:
  21. "{productName} -> {totalPrice}"
  22. • Format the average grade to the 2nd digit after the decimal separator.
  23.  
  24. Examples:
  25. Input:              Output:
  26. Beer 2.20 100       Beer -> 220.00
  27. IceTea 1.50 50      IceTea -> 75.00
  28. NukaCola 3.30 80    NukaCola -> 264.00
  29. Water 1.00 500      Water -> 500.00
  30. buy
  31.  
  32. Input:              Output:
  33. Beer 2.40 350       Beer -> 660.00
  34. Water 1.25 200      Water -> 250.00
  35. IceTea 5.20 100     IceTea -> 110.00
  36. Beer 1.20 200
  37. IceTea 0.50 120
  38. buy
  39.  
  40. Input:                  Output:
  41. CesarSalad 10.20 25     CesarSalad -> 255.00
  42. SuperEnergy 0.80 400    SuperEnergy -> 320.00
  43. Beer 1.35 350           Beer -> 472.50
  44. IceCream 1.50 25        IceCream -> 37.50
  45. buy
  46. """
  47. products = {}
  48.  
  49. while True:
  50.     command = input().split()
  51.     if command[0] == "buy":
  52.         break
  53.     product = command[0]
  54.     price = float(command[1])
  55.     quantity = int(command[2])
  56.     if product not in products:
  57.         products[product] = [price, quantity]
  58.     else:
  59.         products[product][0] = price
  60.         products[product][1] += quantity
  61.  
  62. for product, value in products.items():
  63.     print(f"{product} -> {(value[0] * value[1]):.2f}")
Add Comment
Please, Sign In to add comment