Advertisement
viligen

shopping_list_func

Jan 27th, 2022
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. def shopping_list(budget, **kwargs):
  2.     bought_items = {}
  3.     result = ''
  4.     if budget < 100:
  5.         return f"You do not have enough budget."
  6.     while budget > 0 and len(bought_items) <= 5:
  7.         for item, data in kwargs.items():
  8.             price, quantity = data[0], data[1]
  9.             if price * quantity > budget:
  10.                 continue
  11.             bought_items[item] = price * quantity
  12.             budget -= price * quantity
  13.             if budget == 0 or len(bought_items) == 5:
  14.                 break
  15.         break
  16.     for b_item, value in bought_items.items():
  17.         result += f'You bought {b_item} for {value:.2f} leva.'+'\n'
  18.     return result
  19.  
  20.  
  21. print(shopping_list(104,
  22.                     cola=(1.20, 2),
  23.                     candies=(0.25, 15),
  24.                     bread=(1.80, 1),
  25.                     pie=(10.50, 5),
  26.                     tomatoes=(4.20, 1),
  27.                     milk=(2.50, 2),
  28.                     juice=(2, 3),
  29.                     eggs=(3, 1),
  30.                     ))
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement