So yeah, my function isnt calculating properly. Ex. my deque looks like ([(50, 10), (30, 15), (100, 20)]) with the left num being the amount of shares bought and the right num being the price they were bought at. I sell 70 shares at 18$ it should calc to be 460$, but its calculating 400? Any idea whats messing me up here? Heres my function:
def calculate_capital_gain(amount, price, current_deque):
"""
Calculates the capital gain on certain number of shares at a certain price
:param amount: The amount of shares the user wishes to sell.
:param price: The price the user would like to sell those shares at.
:param current_deque: Allows for importing of deque containing shares and their prices (from buy_shares function)
:return: The total gain from said shares at said amount.
"""
total_gain = 0
while amount > 0:
if len(current_deque) == 0:
return print("Can't work on an empty deque")
else:
raw_num_shares, raw_original_price = current_deque.pop() # Returns the last two values and deletes them, but stores them briefly for calculation purposes
num_shares = int(raw_num_shares) # Change to ints to prevent error from str input
original_price = int(raw_original_price)
if num_shares <= amount:
total_gain = total_gain + (price - original_price) * num_shares # Capital gain formula
amount = amount - num_shares # Reduce amount
else:
total_gain = total_gain + (price - original_price) * amount
amount = 0
return total_gain