MCPcopy Create free account
hub / github.com/dabeaz-course/practical-python / portfolio_cost

Function portfolio_cost

Solutions/1_33/pcost.py:4–22  ·  view source on GitHub ↗

Computes the total cost (shares*price) of a portfolio file

(filename)

Source from the content-addressed store, hash-verified

2
3import csv
4def portfolio_cost(filename):
5 '''
6 Computes the total cost (shares*price) of a portfolio file
7 '''
8 total_cost = 0.0
9
10 with open(filename, 'rt') as f:
11 rows = csv.reader(f)
12 headers = next(rows)
13 for row in rows:
14 try:
15 nshares = int(row[1])
16 price = float(row[2])
17 total_cost += nshares * price
18 # This catches errors in int() and float() conversions above
19 except ValueError:
20 print('Bad row:', row)
21
22 return total_cost
23
24import sys
25if len(sys.argv) == 2:

Callers 1

pcost.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected