42 lines
1.2 KiB
Markdown
42 lines
1.2 KiB
Markdown
\[ [Index](index.md) | [Exercise 1.2](ex1_2.md) | [Exercise 1.4](ex1_4.md) \]
|
|
|
|
# Exercise 1.3
|
|
|
|
*Objectives:*
|
|
|
|
- Review basic file I/O
|
|
|
|
*Files Created:* `pcost.py`
|
|
|
|
## (a) Working with files
|
|
|
|
The file `Data/portfolio.dat` contains a list of lines with information
|
|
on a portfolio of stocks. The file looks like this:
|
|
|
|
```
|
|
AA 100 32.20
|
|
IBM 50 91.10
|
|
CAT 150 83.44
|
|
MSFT 200 51.23
|
|
GE 95 40.37
|
|
MSFT 50 65.10
|
|
IBM 100 70.44
|
|
```
|
|
|
|
The first column is the stock name, the second column is the number of
|
|
shares, and the third column is the purchase price of a single share.
|
|
|
|
Write a program called `pcost.py` that opens this file, reads
|
|
all lines, and calculates how much it cost to purchase all of the shares
|
|
in the portfolio. To do this, compute the sum of the second column
|
|
multiplied by the third column.
|
|
|
|
\[ [Solution](soln1_3.md) | [Index](index.md) | [Exercise 1.2](ex1_2.md) | [Exercise 1.4](ex1_4.md) \]
|
|
|
|
----
|
|
`>>>` Advanced Python Mastery
|
|
`...` A course by [dabeaz](https://www.dabeaz.com)
|
|
`...` Copyright 2007-2023
|
|
|
|
. This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)
|