Jan-26-2022, 03:59 PM
Hello,
I am trying to understand what is going on:
According to the errors output:
Thank you
I am trying to understand what is going on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Portfolio: def __int__( self ): self .holdings = {} #key = ticker, Value = number of shares def buy( self , ticker, shares): self .holdings[ticker] = self .holdings.get(ticker, 0 ) + shares def sell( self , ticker, shares): self .holdings[ticker] = self .holdings.get(ticker, 0 ) - shares def __iter__( self ): return iter ( self .holdings.items()) p = Portfolio() p.buy( 'Alpha' , 15 ) p.buy( 'Beta' , 23 ) p.buy( 'Gamma' , 9 ) p.buy( 'Gamma' , 20 ) p.sell( 'Beta' , 5 ) for (ticker, shares) in p: print (ticker, shares) |
Error:Traceback (most recent call last):
File "<string>", line 19, in <module>
File "<string>", line 9, in buy
AttributeError: 'Portfolio' object has no attribute 'holdings'
>
I am missing a module? But which one?Thank you