3

Quick question:

I want to call a main function without any arguments, and within the main function, I have several other functions that do have arguments. How would I go about doing this?

Here are the multiple functions:

 # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
 # the total price of the shares at time of purchase

def total_purchase_price(portfolio):
    totalprice = 0
    totalpurprice = 0
    for item in portfolio:
        purdate, purprice, numshares, sym, curprice = item
        totalprice += purprice * numshares
        totalpurprice = totalprice
    return totalpurprice

# Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
# the current total value of the shares

def total_value(portfolio):
    totalprice = 0
    totalvalueprice = 0
    for item in portfolio:
        purdate, purprice, numshares, sym, curprice = item
        totalprice += curprice * numshares
        totalvalueprice = totalprice
    return totalvalueprice   

# Takes the previous two functions, and subtracts them to get the total
# gain/lost of the shares

def total_gain(total_purchase_price, total_value, portfolio):
    gainlost = total_value - total_purchase_price
    return gainlost

ex. What I have right now (note: I know this won't work, just there for what I want, as each function returns a value):

def testQ1(total_purchase_price, total_value, total_gain, portfolio):
    print("Total Cost =", total_purchase_price)
    print("Current Value =", total_value)
    print("Total gain/lost =", total_gain)
    return

ex. What I want to achieve:

def testQ2():
    total_purchase_price(portfolio)
    total_value(portfolio)
    total_gain(total_purchase_price, total_value, portfolio)
    print("Total Cost =", total_purchase_price)
    print("Current Value =", total_value)
    print("Total gain/lost =", total_gain)
    return   

How would I do this? Thanks

4
  • Are these values global and where are they initialized. Would testQ2() read them from a file and have another function that returns them? Commented Mar 7, 2016 at 21:13
  • You don't assign the return values to variables. Commented Mar 7, 2016 at 21:15
  • testQ2 needs to know what portfolio is, as sabbahillel says, you should use global vars OR make a class and then they can be an attribute to the class Commented Mar 7, 2016 at 21:35
  • 1
    I explain how to set up the variables in my answer Commented Mar 7, 2016 at 21:50

2 Answers 2

3

Using a class might be easier:

class PortfolioParser:
    def __init__(self, portfolio):
        self.portfolio = portfolio
        self.price = self.total_purchase_price()
        self.value = self.total_value()
        self.gain = self.total_gain()

    def total_purchase_price(self):
        # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
        # the total price of the shares at time of purchase
        totalprice = 0
        totalpurprice = 0
        for item in self.portfolio:
            purdate, purprice, numshares, sym, curprice = item
            totalprice += purprice * numshares
            totalpurprice = totalprice
        return totalpurprice      

    def total_value(self):
        # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
        # the current total value of the shares
        totalprice = 0
        totalvalueprice = 0
        for item in self.portfolio:
            purdate, purprice, numshares, sym, curprice = item
            totalprice += curprice * numshares
            totalvalueprice = totalprice
        return totalvalueprice   

    def total_gain(self):
        # Takes the previous two functions, and subtracts them to get the total
        # gain/lost of the shares
        return self.value- self.price

    def testQ2(self):
        print("Total Cost = {0}\nCurrent Value = {1}\nTotal Gains = {2}".format(self.price, self.value, self.gain))

And then you would use it like so:

myPortfolio = # The portfolio to parse
parser = PortfolioParser(myPortfolio)
parser.testQ2()
Sign up to request clarification or add additional context in comments.

Comments

1

portfolio is the only argument that is not calculated within testQ2. You need to have a global value for it, or read it in (maybe in another function). After they have been calculated return the values in an appropriate order.

def testQ2():
    portfolio = getPortfolio()
    tp = total_purchase_price(portfolio)
    tv = total_value(portfolio)
    tg = total_gain(tp, tv, portfolio)
    print("Total Cost =", tp)
    print("Current Value =", tv)
    print("Total gain/lost =", tg)
    return portfolio, tp, tv, tg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.