This commit is contained in:
David Beazley
2020-05-27 21:13:24 -05:00
parent 5c663c8b19
commit f9717a12c6
5 changed files with 110 additions and 73 deletions

31
Solutions/1_33/pcost.py Normal file
View File

@@ -0,0 +1,31 @@
# pcost.py
import csv
def portfolio_cost(filename):
'''
Computes the total cost (shares*price) of a portfolio file
'''
total_cost = 0.0
with open(filename, 'rt') as f:
rows = csv.reader(f)
headers = next(rows)
for row in rows:
try:
nshares = int(row[1])
price = float(row[2])
total_cost += nshares * price
# This catches errors in int() and float() conversions above
except ValueError:
print('Bad row:', row)
return total_cost
import sys
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = input('Enter a filename:')
cost = portfolio_cost(filename)
print('Total cost:', cost)