Skip to content

Commit 7485597

Browse files
committed
Updated to Exercise 1.30: Turning a script into a function
1 parent d7b4bc8 commit 7485597

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎Work/pcost.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# pcost.py
22
#
33
# Exercise 1.27
4-
f = open('Data/portfolio.csv', 'r')
5-
next(f)
6-
s = 0
7-
for line in f:
8-
row = line.strip().split(',')
9-
s += int(row[1]) * float(row[2])
10-
print(f'Total cost: {s}')
4+
def portfolio_cost(filename):
5+
s = 0
6+
with open(filename, 'r') as f:
7+
for line in f:
8+
row = line.split(',')
9+
try:
10+
s += int(row[1]) * float(row[2])
11+
except ValueError:
12+
print(f"Couldn't calculate line: {line}")
13+
return s
14+
15+
cost = portfolio_cost('Data/portfolio.csv')
16+
print(f'Total cost: {cost}')

0 commit comments

Comments
 (0)