Help, I just started programming and I'm stuck with one part of my code. I need to create a program that will track your expenses, so you input your budget, how much you want to save, and then what you want to spend it on. So first I need to figure out a way to create a priority within those wants and then start subtracting it to the amount of money left but it has to stop when you don't have enough money for something and warn you that you cannot spend money on that. Any suggestions?
This is what I had thought of, but it's wrong as it only checks the first value from the dictionary and skips the rest. Help!!
def gexpenses(spared,wants):
y = 0
while (y <= spared):
for value in wants.values():
y = spared - value
return y
want = {"Party": 10, "Trip": 10, "Clothes":10}
spare = 25
print(expenses(25,want))
CodePudding user response:
You could consider using a Priority Queue (look up the heapq library) in Python. A Priority Queue is basically a data structure that will always produce the item with the minimum (or maximum) priority.
You would have to assign a priority number to each of your wants along with the associated cost to it. You can then remove the item from the Priority Queue and deduct the cost associated with that item. If including that item's cost exceeds your budget, add that item back into the Priority Queue. The Priority Queue will automatically shuffle that item back to the top of the "Queue".
Edited (Added Sample Code as per comments)
# Import Priority Queue library
from heapq import heappush, heappop
# Initialise remaining budget, wants, priority queue (heap)
# For the list of wants, initialise with (priority, item, cost)
# Initialise a list of affordables, assuming you'll want to see what you can afford
remaining_budget = 20
wants = [(2, "Party", 10), (1, "Clothes", 10), (3, "Trip", 10)]
heap = []
affordables = []
# The priority queue implemented in Python is a minimum heap,
# meaning that the smallest number will be "pushed" out of the priority queue first
for i in wants:
heappush(heap, i)
def get_expenses(remaining_budget, heap, affordables):
have_budget = 1
while have_budget:
item = heappop(heap)
# Check if this item causes us to exceed the budget
if remaining_budget - item[2] < 0:
# If it does, put it back in the heap
heappush(heap, item)
have_budget = 0
else:
remaining_budget -= item[2]
affordables.append(item)
return remaining_budget, affordables
remaining_budget, affordables = get_expenses(remaining_budget, heap, affordables)
print("Items you can buy: ", [i[1] for i in affordables])
print("Remaining Budget: ", remaining_budget)
Based on the above, the sample output is as follows:
Items you can buy: ['Clothes', 'Party']
Remaining Budget: 0
Since Clothes and Party have a "higher" priority (lower priority value) than Trip, Clothes and Party are purchased first.
The sample code is probably not streamlined/optimized, but you could read up more about how to implement a Priority Queue and adjust your code based on the requirements of your program.