forked from dabeaz-course/python-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcost.py
32 lines (25 loc) · 893 Bytes
/
pcost.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import stock
def portfolio_cost(filename):
running_total = 0.0
with open(f'{filename}') as f:
for line in f:
name, quant, price = line.split()
try:
running_total += int(quant) * float(price)
except ValueError as e:
print("Couldn't pass", repr(line))
print('Reason:', e)
return f'${running_total}'
def portfolio(filename):
with open(f'{filename}') as f:
for line in f:
name, quant, price = line.split()
try:
s = stock.Stock(name, int(quant), float(price))
except Exception as e:
print(e)
s = stock.Stock('error', 0, 0)
print(f'{s.name}, cost: ${s.cost()}')
if __name__ == '__main__':
print(portfolio('Data/portfolio.dat'))
print(portfolio_cost('Data/portfolio.dat'))