Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

junglebooboo

  • 3 years ago

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

  • This Question is Open
  1. junglebooboo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  2. junglebooboo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #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()

  3. junglebooboo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Any feedback will be greatly appreciated=)

  4. rsmith6559
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy