Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b199039

Browse files
committed
Completed Exercise 1.27: Calculate total cost of all stocks
1 parent 72dcfd9 commit b199039

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Work/pcost.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
# pcost.py
22
#
33
# Exercise 1.27
4+
5+
# Initialise total to R0.0
6+
total_amount = 0.0
7+
with open('Data/portfolio.csv', 'rt') as file:
8+
# Discard the titles
9+
headers = next(file)
10+
# Iterate over the stocks and sum all
11+
for line in file:
12+
stock_line = line.split(sep=',')
13+
shares = int(stock_line[1])
14+
price = float(stock_line[2])
15+
total_amount += shares * price
16+
17+
print(f'Total cost: R{total_amount:.2f}')

0 commit comments

Comments
 (0)