Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Help with python stock portfolio tracker first meeting with objects on my own

OpenStudy (anonymous):

Hi I'm trying to make a python stock portfolio tracker, so far I have made two attempts, and got stuck at both of them, any tips? #1 # Stock portfolio tracker import datetime def welcome(): print "Welcome to the stock portfolio tracker" print "Let's hope it works this time" class Stock(object): # Creates and saves a portfolio of stocks def __init__(self, date, price, number_of_shares): # Initialises the portfolio and takes three arguments self.date = date self.price = price self.number_of_shares = number_of_shares def __str__(self): # Creates a string representation of the portfolio # and prints requested values return "date purchased :%s, Price: %s, Number_of_shares %s" %( self.date, self.price, self.number_of_shares) def getPurchaseValue(self): # Computes the purchase value as price per share x number of shares return (self.price * self.number_of_shares) def getSaleValue(self, salePrice): # Computes the sale value as price per share * shares return (self.salePrice * number_of_shares) class Portfolio(Stock): # Creates a portfolio of collected stocks def __init__(self, stockName): self.stockName = stockName portfolio = [] def addStock(self): newStock = raw_input('What is the company ticker?') name = raw_input('Enter the ticker of the stock to add') price = raw_input('Enter the price of the stock') shares = raw_input('How many shares to add?') newstock[name, price, shares] def main(): welcome() MSFT = Stock(10,10,10) print MSFT

OpenStudy (anonymous):

#2 # New attempt at creating a portfolio of stocks class DataBook(object): def __init__(self, price, shares, date): self.price = price self.shares = shares self.date = date self.get_info() def get_info(self): sf = \ """ price = %s shares = %s date = %s """ return sf %(self.price, self.shares, self.date) def main(): name_dic = {} ticker = raw_input("What is the company's ticker?") price = int(raw_input('What was the price?')) shares = raw_input('how many shares did you buy?') date = raw_input(' When did you buy it?') name_dic[ticker] = DataBook(price, shares, date) main()

OpenStudy (anonymous):

Any feedback will be greatly appreciated=)

OpenStudy (rsmith6559):

In Stock.getSaleValue() where does self.salePrice come from? In Portfolio.addStock, what is newstock[name, price, shares]? Square brackets are for lists, parentheses are for functions. DataBook.get_info has a formatted string, but no variables substituted in. main() looks like it would be setting up a dictionary, but name_dic is a list. Advice: Develop you classes independently. It helps to develop/debug them one at a time, and if you're hoping to reuse them it forces you to make them separate.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!