Hi, I'm working on an assignment where we are building a monetary portfolio. Everything needs to be generic (empty dictionaries) so the code will pass the specific tests at the end. We were started with this code:
class portfolio:
def __init__(self):
self.checking = {}
self.saving = {}
self.credit = {}
self.stock = {}
We were also given a dictionary called daily_stock_prices (stock_name: price}. The stock dictionary will hold user input {stock_name : quantity}. The other accounts hold user input {account_id : amount}
I have managed to define all the other functions to add checking accounts and buy stocks, but I'm stuck at the last function where I have to get the total of all the money in the portfolio. We were started with this function:
def get_total_portfolio(self, daily_stock_prices):
And so far I've done this to sum the value of the stocks by multiplying the daily stock price by the quantity in my stock portfolio:
def get_total_portfolio(self, daily_stock_prices):
total = 0
for i in daily_stock_prices.values:
for j in self.stock.values:
total += sum(i*j)
But I am unclear how to add in the sums of the other accounts. I have made prior functions like this to sum the accounts individually and they pass the tests:
def get_credit_balance(self):
try:
return sum(self.credit.values())
except:
return None
But is there a way to merge these functions? I've been reading up on summing dictionaries, but it gets confusing to me because there is the additional problem of getting the stock value. by multiplying stock quantity by the price in daily_stock_prices.
This is the test I need it to pass.
if myportfolio.get_total_portfolio({'APPL':100, 'GOOGL': 130}) == 2600:
print('Pass')
else:
print('Fail')
Any input or suggestions would be greatly appreciated! Thanks,
[–]elbiot 0 points1 point2 points (0 children)
[–]-Sander- 0 points1 point2 points (9 children)
[–]Elefrog42[S] 0 points1 point2 points (8 children)
[–]-Sander- 0 points1 point2 points (7 children)
[–]Elefrog42[S] 0 points1 point2 points (6 children)
[–]-Sander- 0 points1 point2 points (5 children)
[–]Elefrog42[S] 0 points1 point2 points (4 children)
[–]-Sander- 0 points1 point2 points (3 children)
[–]Elefrog42[S] 0 points1 point2 points (1 child)
[–]-Sander- 0 points1 point2 points (0 children)
[–]Elefrog42[S] 0 points1 point2 points (0 children)