CS8, 10F, H10, due Tue Lecture 11.02—Miller/Ranum, first part of 5.2 (pp. 155-160).—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H10—printable PDF

Name: (4 pts)   Umail Address: (4 pts)   @umail.ucsb.edu
Lab Section (2 pts)—circle one:  9am   10am   11am   noon   unknown   crashing 
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here:  
  (He/she should also enter your name on her/his assignment.)

This assignment is due in Lecture on Tuesday, 11.02.
It may ONLY be submitted in Lecture, at 2pm on Tuesday.

You must come IN PERSON to turn it in during your assigned Lecture section.



The reading assignment for this homework is the first part of Section 5.2 in your textbook, namely pages 155-160 which covers reading from data files.

This homework assignment is also preparation for lab07.

Please read pages 155-160, then answer the following questions.

  1. Suppose you want to work with a file called majors.txt that contains data about UCSB majors. You want to use a variable called fileref to be the reference to that file. There is a function that you have to call before you can start working with this file—a function that opens up the file so you can get the data out.

    1. (10 pts) What is the name of that function?


    2. (10 pts) Following the example from the textbook, to use that function, we put fileref on the left-hand side of an assignment statement, and a call to that function on the right hand side of an assignment statement, passing in "majors.txt" and one other parameter—one that indicates we want to read from the file.

      Write the full assignment statement that calls this function below—using "majors.txt", fileref, and the parameter that indicates we want to read—all in the appropriate places.




    3. (10 pts) Similarly, there is a function you need to call when you are finished working with the file—that function is actually a method, and so it is called with dot notation (similar to the dot notation we used for working with cTurtle objects like fred and gertrude the turtles.)

      What is the actual method call going to be in this case of working with fileref and "majors.txt" ?

      (Note that only one of those appears in the correct answer—the other one is implied. Read pages 155-157 in the text textbook to learn which one.)




Please turn over for more...

...continued from other side

  1. (10 pts) On p. 158, in listing 5.1 we see that we can use a for loop to iterate over lines in an input file—similar to the way we iterate over the values in a list.

    We also see that the split() method of a string can be used to turn a line that is separated by spaces into a list of values—e.g. split() turns "Akron 25.81" into ["Akron","25.81"]

    We then use values[0] to access the city from each line, and values[1] to access the rainfall from each line.

    Rewrite listing 5.1 so that it uses the accumulator pattern on two different variables called cities and rainAmounts.

    Initialize each of them to the empty list, i.e.

    cities = []
    rainAmounts = []


    Then, inside the loop, instead of printing out the rainfall amounts, add lines of code that add each city to the cities list, and each rain amount to the rainAmounts list, so that when the loop is finished:
    • cities contains ["Akron","Albia","Algona", etc... ]
    • rainAmounts contains ["25.81","37.65","30,69", etc... ]

End of H10