Skip to content
Advertisement

Novice Python User having issues with simple functions

I am trying to make a simple python program that takes 3 inputs

  1. Number of stocks I will Buy
  2. Price per share of stock when bought
  3. Price per share of stock when sold.

In this exercise, there is a 15% commission fee both when buying, and selling the stock.

The 4 outputs I am attempting to receive are:

  1. Price of bought stock (price per share when bought * number of shares)
  2. Commission on the bought stock
  3. Price of sold stock (price per share when sold * number of shares)
  4. Commission on the sold stock.

I am attempting to do this in a way that utilizes functions to stay organized. I realize that functions are not strictly necessary for this program, but the point of this exercise is to help me understand how to use functions a little better.

When I run my program, I receive this Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "main.py", line 6, in main
    pcos = calcAmount(n, ppsb)
TypeError: calcAmount() missing 1 required positional argument: 'ppss'

Here is my program:

def main():
    com = 0.15
    n = float(input("How many shares are in this block of stock?n"))
    ppsb = float(input("What was the price-per-share when you BOUGHT the stock?n"))
    ppss = float(input("What was the price-per-share when you SOLD the stock?n"))
    pcos = calcAmount(n, ppsb)
    pcom = calcCommission(com, pcos)
    arec = calcAmount(n, ppss)
    scom = calcCommission(com, arec)
    print("Purchase Cost: " + str(pcos) + "nPurchase Commission: " + str(pcom) + "nAmount Received from Sale: " + str(arec) + "nSelling Commission: " + str(scom))
# Ask for share #,PSS bought, and PSS sold.

def calcAmount(n,ppsb,ppss):
  pcos = n * ppsb
  return pcos
  arec = n * ppss
  return arec
# 

def calcCommission(com,pcos,arec):
    pcom = com * pcos
    return pcom
    scom = com * arec
    return scom
  # Calculate Commission

The indention when copying / pasting to stack overflow ended up a little wonky. I’m not sure how to fix that.

I apologize if there are any egregious formatting errors, I am new to Python and Stack Overflow.

Advertisement

Answer

def main():
    com = 0.15
    n = float(input("How many shares are in this block of stock?n"))
    ppsb = float(input("What was the price-per-share when you BOUGHT the stock?n"))
    ppss = float(input("What was the price-per-share when you SOLD the stock?n"))
    # we're passing three arguments to the function, as it needs all three
    pcos, arec = calcAmount(n, ppsb, ppss)
    # same here, I've added 'arec' argument the the function call
    pcom, scom = calcCommission(com, pcos, arec)
    print("Purchase Cost: " + str(pcos) + "nPurchase Commission: " + str(pcom) + "nAmount Received from Sale: " + str(arec) + "nSelling Commission: " + str(scom))
# Ask for share #,PSS bought, and PSS sold.

def calcAmount(n,ppsb,ppss):
    pcos = n * ppsb
    # we've removed 'return' statement, since code after it won't be executed
    arec = n * ppss
    # we're returning both values at the same time, and unpack them at the function call
    return pcos, arec


def calcCommission(com,pcos,arec):
    pcom = com * pcos
    # same here, instead of calling 'return' twice (which won't work)
    # we're returning both values at the same time as a tuple
    # a,b = func()
    # if function is called in this manner
    # python will unpack retuned values and assign them to a and b
    scom = com * arec
    return pcom, scom

Good luck with python! :)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement