Skip to content

Commit 8ff64c6

Browse files
committed
added csv module
1 parent eb218fa commit 8ff64c6

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

‎Work/pcost.py‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# pcost.py
22
#
33
# Exercise 1.27
4-
4+
import csv
55
def portfolio_cost(filename):
6-
shares = 0
7-
price = 0
8-
total = 0
9-
f = open(filename)
10-
headers = next(f)
11-
for line in f:
12-
row = line.split(',')
13-
shares = row[1]
14-
price = row[2]
15-
total += float(price) * int(shares)
16-
f.close()
17-
return total
6+
try:
7+
shares = 0
8+
price = 0
9+
total = 0
10+
f = open(filename)
11+
rows = csv.reader(f)
12+
headers = next(rows)
13+
for row in rows:
14+
#i = row.split(',')
15+
shares = row[1]
16+
price = row[2]
17+
total += float(price) * int(shares)
18+
f.close()
19+
return total
20+
except ValueError:
21+
print("Doesn't look good, dawg", line)
1822
cost = portfolio_cost('Data/portfolio.csv')
1923
print('Total cost: ', cost)

0 commit comments

Comments
 (0)