Skip to content

Commit 9012611

Browse files
committed
Handle broken input with try/except
Example to test: python pcost.py Data/missing.csv
1 parent a8ec716 commit 9012611

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Work/pcost.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ def portfolio_cost(filename):
77
with open(filename) as csvfile:
88
reader = csv.DictReader(csvfile)
99
for row in reader:
10-
total_cost += int(row['shares']) * float(row['price'])
10+
try:
11+
total_cost += int(row['shares']) * float(row['price'])
12+
except ValueError as err:
13+
print(f'Invalid line at name {row["name"]}'
14+
f'\nThe line is not used in the computation of Total cost'
15+
f'\nErrormessage: {err}')
1116
return total_cost
1217

1318
if __name__ == '__main__':

0 commit comments

Comments
 (0)