Calling Function in Function without Arguments
12,611
Solution 1
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()
Solution 2
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
Author by
Saigo no Akuma
Updated on June 04, 2022Comments
-
Saigo no Akuma almost 2 years
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