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

Function portfolio_cost

Solutions/1_4/pcost.py:3–18  ·  view source on GitHub ↗
(filename)

Source from the content-addressed store, hash-verified

1# pcost.py
2
3def portfolio_cost(filename):
4 total_cost = 0.0
5 with open(filename) as f:
6 for line in f:
7 fields = line.split()
8 try:
9 nshares = int(fields[1])
10 price = float(fields[2])
11 total_cost = total_cost + nshares*price
12
13 # This catches errors in int() and float() conversions above
14 except ValueError as e:
15 print("Couldn't parse:", repr(line))
16 print("Reason:", e)
17
18 return total_cost
19
20print(portfolio_cost('../../Data/portfolio3.dat'))

Callers 1

pcost.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected