#๐Ÿ”’ Capital Gain Calculator Not Calculating Properly (Using Deque)

7 messages ยท Page 1 of 1 (latest)

past bridge
#

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
hazy cobaltBOT
#

Hey @past bridge!

Please edit your message to use a code block

You are using the wrong character instead of backticks. Use ```, not """.

hazy cobaltBOT
#

@past bridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

safe phoenix
#

Put in some print() calls to show the values popped from the deque, and also the results of the calculations. May make things more clear.

past bridge
#

ok good idea ill try that thanks

hazy cobaltBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.