ex 1.4
This commit is contained in:
parent
784349ec36
commit
73cbc2de2a
20
pcost.py
20
pcost.py
@ -1,17 +1,19 @@
|
|||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
|
def portfolio_cost(filename: str) -> float:
|
||||||
def run(filename: str) -> Decimal:
|
|
||||||
with open(filename, 'r') as lines:
|
with open(filename, 'r') as lines:
|
||||||
total = Decimal(0.0)
|
total = 0.0
|
||||||
_, count, price = lines.readline().split()
|
for line in lines:
|
||||||
count = Decimal(count)
|
_, count, price = line.split()
|
||||||
price = Decimal(price)
|
try:
|
||||||
total += count * price
|
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
|
return total
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
filename = 'Data/portfolio.dat'
|
filename = 'Data/portfolio.dat'
|
||||||
value = run(filename)
|
value = portfolio_cost(filename)
|
||||||
print(f"value = {value}")
|
print(f"value = {value}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user