diff --git a/pcost.py b/pcost.py index 30e7e65..18c72c6 100644 --- a/pcost.py +++ b/pcost.py @@ -1,17 +1,19 @@ -from decimal import Decimal - -def run(filename: str) -> Decimal: +def portfolio_cost(filename: str) -> float: with open(filename, 'r') as lines: - total = Decimal(0.0) - _, count, price = lines.readline().split() - count = Decimal(count) - price = Decimal(price) - total += count * price + total = 0.0 + for line in lines: + _, count, price = line.split() + try: + count = int(count) + price = float(price) + total += count * price + except ValueError as ex: + print(f"Couldn't parse {line!r}. Reason: {ex!r}") return total if __name__ == '__main__': filename = 'Data/portfolio.dat' - value = run(filename) + value = portfolio_cost(filename) print(f"value = {value}")