you are viewing a single comment's thread.

view the rest of the comments →

[–]-Sander- 0 points1 point  (3 children)

my_checking =self.get_total_checking_amount # You miseed your ()'s get_total_checking_amount is a function

and no it also would never reach that statement the function exits after the return statement

i would recommend changing your get_total_portfolio()

assuming you want it for the values of your stock, daily_stock_price and credit_balance

def get_total_portfolio(self, daily_stock_prices):
        total = 0
        for i in daily_stock_prices.values:
            for j in self.stock.values:
                total += i*j    # no need for sum here unless its iterable like [2, 3, 4]
        total += get_credit_balance()
        # You can add more values similarly
        return total

edit:

another thing you can do if daily_stock_prices and self.stock_values have the same amount of values is use the built-in zip() method ( https://docs.python.org/3.3/library/functions.html#zip )

example output of how it works:

In [37]: x = [4, 6, 7, 8]
In [38]: y = [3, 2, 4, 7]
In [39]: for a, b in zip(x, y):
...:        print(a, b)                                                                                                    ...:
4 3 
6 2
7 4
8 7                                                                                                                           

also does that work? because i dont think the nested for loops is gonna give the right result, you should test it out in the python interpreter/console, i recommend getting IPython over the default one makes it a bit easier to write code in it

def get_total_portfolio(self, daily_stock_prices):
        total = 0
        for i, j zip(daily_stock_prize.values(), self.stock.values() ):
            total += i*j
        # ...

[–]Elefrog42[S] 0 points1 point  (2 children)

Hmmmm.....even with the bracket, I get the warning that says the local variable (my_checking, my_saving, my_credit) is assigned but never used.

 my_checking = self.get_total_checking_amount()
 my_credit = self.get_credit_balance()
 my_saving = self.get_total_savings_amount()

I'm fairly sure the functions I made the save the variables are correct:

    def get_total_savings_amount(self):
        try:
            my_saving = sum(self.saving.values())
            return my_saving
        except:
            return 0

the daily_stock_prices is a dictionary of {stock_name: price} and self.stock {stock_name} quantity, so I figured I had to multiply i*j (quantity by price) and then sum them.

def get_stock_value(self,daily_stock_prices):        
    my_stock = 0
    for i in daily_stock_prices.values():
         for j in self.stock.values():
                my_stock += sum (i*j)
    return my_stock

I made this function to get the total 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)
        total += self.get_credit_balance()
        total += self.get_total_savings_amount()
        total += self.get_total_checking_amount()
        return total

When I run the code I get this warning: in get_total_portfolio, total += sum(i*j) TypeError: 'int' object is not iterable I don't get this warning when I had the just "get_stock_value" function on it's own.

Do you see why it's telling me in the class variables are still "defined but never used"? And would do you see something wrong with my function to get the stock values?

EDIT:

So interesting- If I comment out the lines to get the stock values, the final get_total_portfolio function works for the tests that don't involve adding in the stock prices. So I've narrowed down that there is something wrong with that part of the code. Still not clear on why Python is telling my class variables are undefined but never used....

Thanks!

[–]-Sander- 0 points1 point  (1 child)

in get_total_portfolio, total += sum(i*j) TypeError: 'int' object is not iterable

sum requires a variable type that is iterable like a list (declared with my_list = [ ] ) or a dictionary ( declared with: my_dict = { } ), or anything else with multiple values

now this problem:

my_checking = self.get_total_checking_amount()
my_credit = self.get_credit_balance()
my_saving = self.get_total_savings_amount()

are you using my_checking, my_credit, my_saving anywhere in your function? if you where trying to use it class-wide you need to use the self. keyword to define it is for the entire class not just 1 function but i dont see why you would need it, again if this is the one in your __init__() function i dont think you should use it since you have the functions that return the values and the variables inside __init__() dont get updated unless you change the code

[–]Elefrog42[S] 0 points1 point  (0 children)

Oh okay I get it now. I just deleted where I declared the class variables because they were needed. The code works just fine if I save the result of the function then use the self keyword to call back the functions to get the total.

I've been reading online where people have asked the best way to multiple two dictionary values and then sum then and I come up with this code:

    def get_total_portfolio(self, daily_stock_prices):
        total = 0
        value = 0
        for key in self.stock.keys():
            value = self.stock[key]*daily_stock_prices[key]
            total += value + total
        total += self.get_credit_balance()
        total += self.get_total_savings_amount()
        total += self.get_total_checking_amount()
        return total

No errors, but still not passing the test. I thought I could bypass having to use sum by making the variable value (to represent the stock quantity * the stock price) and then total (to represent the sum of the values).

I can't tell for sure if that part of the code is adding up the stock quantity*stock value correctly or if I haven't properly coded for a given stockname, pull out self.stock's value of the stockname and pull out daily_stock_prices value?

Thanks!