CS8, 10F, H11, due Fri Lab 11.12—Miller/Ranum, next part of 5.2 (pp. 159-163), output files, string formatting.—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H11—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 Lab on Friday, 11.12.
It may ONLY be submitted in Lab, in ESB1003 (Cooper Lab) on Friday.

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



This homework assignment is also preparation for lab09.

The reading assignment for this homework continues the reading in section 5.2 in your textbook, covering writing to files (section 5.2.2, pp. 159-160) and string formatting (section 5.2.3, pp. 161-163).

Please note this typo on p. 162: the fifth example in Table 5.3 should read %20.2f to be consistent with the description.
Please read those sections, then answer these questions:

  1. Suppose that instead of printing out results to the screen, you want to write information out to a file on the disk. You might, for example, be writing information into a file that will ultimately become a web page, or into a file that you are going to open with a program such as Microsoft Excel, or a Google Documents Spreadsheet.

    1. For this question, write ONLY the NAME of the function, not a full function call.

      (5 pts) Just like with reading in a data from an input file, if you want to work with an output file, there is a function you have to call to "get started". What is the name of that function that you use to get started with both input and output files?




    2. For this question, write a full assignment statement.

      (5 pts) Now, lets write a full assignment statement that uses that function to set up the variable reportFile as the file object that you'll use to write information into a file that will be called report.txt in your Unix directory, or on your computer's hard drive.



    3. (5 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 reportFile and report.txt ?





  2. Note that there are at least two important differences between the print() function that puts information on the screen, and the .write() method that writes to output files.

    • multiple arguments vs. a single string argument
      • The print() function can take multiple arguments—e.g. if x is number, this is legal: print("x=",x)
      • By contrast,the .write() method can only take one argument, which must be of type string. If we want to write multiple things, we have to convert each one to a string, and use concatentation: e.g. write("x=" + str(x))
    • automatic newline vs. manual newline
      • the print() function always automatically goes to a newline (unless you override this with end="")
      • By contrast the .write() method doesn't go to a newline unless you add "\n" at the end of the string.
    (5 pts) The .write() method is used to write information to a file. Suppose we are writing to the file reportFile and we've already done the assignment statement in question 1b (earlier on this homework.)


    What line of Python code will write the string:

    Fall 2010 Report

    to this file, followed by a newline character?




Please turn over for more...

...continued from other side

  1. You can do this exercise in two ways:

    • You can just type in the stuff in the left column, like a robot, and copy down what appears at the Python prompt.
      You'll get full credit for this assignment, but will learn very little. I don't recommend this technique.

    • Better: read pp. 161-163 and based on what you learn, try to predict what will happen before you hit return. (Perhaps you might write it on a piece of scratch paper.) Then, see if your answer was right. If it wasn't try to figure out where your understanding was mistaken.

    Either way, write the correct answer in the right column below.

    (20 pts) We are already familiar with the use of the % operator for "modulus", i.e the "remainder after integer division" operation.

    That's what the % operator does when the left and right operands are both numbers.

    But, when the left operand of the % operator is a string, the % operator does something completely different.

    Section 5.2.3 (pp. 161-163) describes this whole other use of the % symbol, for something called string formatting—and Session 5.3 (p. 163) shows some examples.

    The best way to learn the string formatting operator is to try a lot of examples. So, here are some.

    For each of the expressions in the left hand column of the table below, determine what the result of that expression will be if you type it in at the Python prompt, and put that in the right hand column—one character per square, starting from the left. The first few are done for you as examples.

    Assume that the following assignment statements have been typed in before these expressions are typed in.

    w = 543
    x = 12.39485
    y = -34
    z = "UCSB"
    
    Expression Result
    "%5d" % w '     5 4 3 '                          
    "%6d" % 123 '       1 2 3 '                        
    "%6s" % z '     U C S B '                        
    "%-12s" % "Isla Vista" ' I s l a   V i s t a     '            
    "w=%5d y=%4d" % (w,y) ' w =     5 4 3   y =   - 3 4 '        
    "%8.2f" % x                                        
    "w=%4d y=%5d" % (w,y)                                        
    "%9.3f" % x                                        
    "%-5s" % z                                        
    "%5s" % z                                        
(Grading: Five problems, four points each, no partial credit—output must be exact.)

End of H11