I am trying to make a simple python program that takes 3 inputs
- Number of stocks I will Buy
- Price per share of stock when bought
- 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:
- Price of bought stock (price per share when bought * number of shares)
- Commission on the bought stock
- Price of sold stock (price per share when sold * number of shares)
- 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:
JavaScript
x
6
1
Traceback (most recent call last):
2
File "<stdin>", line 1, in <module>
3
File "main.py", line 6, in main
4
pcos = calcAmount(n, ppsb)
5
TypeError: calcAmount() missing 1 required positional argument: 'ppss'
6
Here is my program:
JavaScript
1
26
26
1
def main():
2
com = 0.15
3
n = float(input("How many shares are in this block of stock?n"))
4
ppsb = float(input("What was the price-per-share when you BOUGHT the stock?n"))
5
ppss = float(input("What was the price-per-share when you SOLD the stock?n"))
6
pcos = calcAmount(n, ppsb)
7
pcom = calcCommission(com, pcos)
8
arec = calcAmount(n, ppss)
9
scom = calcCommission(com, arec)
10
print("Purchase Cost: " + str(pcos) + "nPurchase Commission: " + str(pcom) + "nAmount Received from Sale: " + str(arec) + "nSelling Commission: " + str(scom))
11
# Ask for share #,PSS bought, and PSS sold.
12
13
def calcAmount(n,ppsb,ppss):
14
pcos = n * ppsb
15
return pcos
16
arec = n * ppss
17
return arec
18
#
19
20
def calcCommission(com,pcos,arec):
21
pcom = com * pcos
22
return pcom
23
scom = com * arec
24
return scom
25
# Calculate Commission
26
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
JavaScript
1
30
30
1
def main():
2
com = 0.15
3
n = float(input("How many shares are in this block of stock?n"))
4
ppsb = float(input("What was the price-per-share when you BOUGHT the stock?n"))
5
ppss = float(input("What was the price-per-share when you SOLD the stock?n"))
6
# we're passing three arguments to the function, as it needs all three
7
pcos, arec = calcAmount(n, ppsb, ppss)
8
# same here, I've added 'arec' argument the the function call
9
pcom, scom = calcCommission(com, pcos, arec)
10
print("Purchase Cost: " + str(pcos) + "nPurchase Commission: " + str(pcom) + "nAmount Received from Sale: " + str(arec) + "nSelling Commission: " + str(scom))
11
# Ask for share #,PSS bought, and PSS sold.
12
13
def calcAmount(n,ppsb,ppss):
14
pcos = n * ppsb
15
# we've removed 'return' statement, since code after it won't be executed
16
arec = n * ppss
17
# we're returning both values at the same time, and unpack them at the function call
18
return pcos, arec
19
20
21
def calcCommission(com,pcos,arec):
22
pcom = com * pcos
23
# same here, instead of calling 'return' twice (which won't work)
24
# we're returning both values at the same time as a tuple
25
# a,b = func()
26
# if function is called in this manner
27
# python will unpack retuned values and assign them to a and b
28
scom = com * arec
29
return pcom, scom
30
Good luck with python! :)