Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.
To turn your work in, go to
mycourses.unh.edu
,
find CS414, open the syllabus, and click on Lab 10. Then click
Submit Assignment, click Choose file, find the file that you
worked on, and upload it.
In case you haven't done so already, go to the course website,
http://cs.unh.edu/~cs414
,
scroll to the bottom, and visit the three links that talk about
loops and file I/O. READ THEM CAREFULLY.
Download the starting program, and the input file:
Then do these exercises:'money.txt'
:
out_handle = open('money.txt', 'w')and writes all the lines into it:
for line in lines: out_handle.write(line)then closes the file:
out_handle.close()
.
Check the file money.txt
. If you did
everything right, it should look just like the original
file growth.txt
.
growth.txt
, and notice that each line has
three fields, separated by commas.
Instead of just writing a line to the output file, do this:
'\n'
newline character at
the end. Remove it:
line.rstrip('\n\r')
fields = line.split(',')
to split
each line into fields.
for
loop to write the three
fields to the output file. Write a ','
comma after each field:
for field in fields: out_handle.write(field + ',')
for
loop, write
a '\n'
newline to the output file, to
replace the one you removed.
money.txt
. If you did
everything right, it should look just like the original
file growth.txt
, although there may be an
extra comma at the end of each line (if so, don't worry;
that's OK).
The first line of the file is the "header". It has the names of the three fields: Year, Balance, and Explanation. Let's add another field, Income, between Balance and Explanation.
Write the first line separately, with an extra field:
out_handle.write('Year,Bank Balance,Income,Explanation\n') for line in lines[1:] : fields = line.split(',') ...The
fields
list has 3 strings. We want to do
some computations, so let's turn some of them into
numbers, after fields
is created:
balance = float(fields[1])Now, we want to compute the income, i.e., the difference between balances. For example in 1995, the income was 1100 - 1080 = 20. In 1996, the income was 1000000.
To do this, start with prev_balance = 1000
,
and do the following, before writing the fields:
income = balance - prev_balance fields.insert(2, income) prev_balance = balanceNow
fields
has three strings, and
one float
. Run the program. It should
crash, because of out_handle.write(field +
',')
. The write()
function expects
a string, but the third field is a float
.
Change that line:
out_handle.write(str(field) + ',')and the program should run correctly.
Add another field to the file: the growth rate. Several parts of the program will change:
Year,Bank
Balance,Income,Rate,Explanation
Compute averages for the balance, the income, and the
growth rate columns. You will have to create three
totals:
total_balance
,
total_income
, and
total_growth
. Initialize these to zero.
As you process each line, add each value into its total:
total_balance += balanceand similarly for income and growth.
Then, after processing all the lines, obtain the average
values: divide these totals by (len(lines) -
1)
.
Finally, add one (or more) final write()
calls
that output the word Averages
in the first
field, then the three averages.
Repeat the previous exercise, but now compute the minimums and maximums for the three columns Balance, Income, and Rate.
Add two lines to the output file, one for the minimums, and one for the maximums.