CS8, 09M, UCSB

H07: (Based on Miller/Ranum, Chapter 3 (pp. 91-92) and Chapter 4 (up to p. 128) Total points: ? (printable PDF)

Accepted: on paper, in lecture (11am in HFH1132) on Tuesday, August 25th
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Lab Section (1 pts) Circle one:         Thu 11am                 Thu 12:30pm                     Unknown

Circle the one you plan to attend, which may or may not be the one you are registered for.


Read pages 155-171 in Chapter 5. Then answer these questions:

  1. (5 pts) If we want to read some data form a file on the disk into our Python program, there is a function call we have to make as our first step. Give an example of this function call:






  2. (5 pts) When we are finished with reading data from a file, there is another function call we should do to indicate that we are finished.

    (Although the boook doesn't go into detail as to why, this step can be very important in large, real-world programs—if you forget to do it, and you end up doing this over and over again in a program that reads data from lots of files, you can eventually cause your program to lock-up or crash.)

    Give an example of the Python function you should call when you are finished with reading data from a file.


  3. If you want to put output in a file on the disk, you use a function call similar to your answer for question 1, but slightly different.

    1. (5 pts) Give an example of this function call


    2. (5 pts) Explain what is different between using that function to prepare to read data from the disk, and to prepare to write data from the disk.



    3. (5 pts) The function you use to finish up writing to the disk has the same form as the one you use to finish up when reading from the disk. What is an example of that funciton call?


    4. (5 pts) In the code, in between the function call you make to "get started" with writing to the disk, and the one you make to "finish up" with writing to the disk, there is a function call that you use to actually put output in the file. Give an example of that function call.



    Please turn over for more problems

    Continued from other side

  4. (10 pts) There is an example on page 158 of reading data from a file that has two pieces of data on each line: the name of a city, and the rainfall amount.

    The program gives lines of output like this:

    Akron had 25.81 inches of rain.
    Albia had 37.65 inches of rain.
    etc...

    Suppose that instead, each line of data had three pieces of data, like this:


    Akron, 25.81, 4.01
    Albia, 37.65, 3.81
    etc ...
    ...where the first two are the same as described in the text, and the last one is the maximum rainfall on a single day during the year.

    Suppose we wanted output like this:

    Akron had 25.81 inches of rain (per day max: 4.01)
    Albia had 37.65 inches of rain (per day max: 3.81)
    To get this effect: only one line in the program would have to change—namely, this one:

    print(values[0], " had ", values[1], " inches of rain.")
    How would you change the line, to get the output indicated above?



  5. (5 pts) Back to reading data for a moment: if you read in a string that has commas separating each of the items, e.g.

    "UCSB, Santa Barbara, CA, Gauchos"



    Suppose that string is stored in a variable called inputLine. What function can you call to assign the variable items to be that data as a list? That is, how can we make the variable items have this value:

    ['UCSB','Santa Barbara','CA','Gauchos']

    Note: I'm looking for an assignment statement with items on the left hand side, and something involving inputLine on the right. I'm not looking for:

    items = ['UCSB','Santa Barbara','CA','Gauchos']
    
    That answer gets no credit.

End of H07