Module 9

Module 9 - Module 9 File input and output Keeping it...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 9 File input and output Keeping it forever CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 9.1 Purpose of Module 9 • Screen output and keyboard input • Parsing input strings • File input and output • Readings: PfSD 8, 12, 14 9.2 1 Screen output and keyboard input Programs for practical use: input and output A big drawback with all of the programs we’ve written: • We can only run our programs by calling functions with the entire data for in the arguments to the function call. • We did see how to do keyboard input, but only very briefly, and we would not want to type in large quantities of data. If we want to make programs that are useful for day-to-day tasks, we’ll need to give them their data by either typing it or supplying it some other way. We also need to be able to show the result of our computations. 9.3 1.1 Screen output and formatting Screen output The only way we know to show anything on the screen is the print command. We can print strings: >> print ’my dog has fleas’ my dog has fleas We can print integers: >> fleacount = 12 >> print fleacount 12 But how would we print out "My dog has 12 fleas" (or whatever the right number is, de- pending on the value of fleacount )? 9.4 1 Structured output The print command can take either a string or anything else as its input. We could build the proper string, and then print it. >> fleastring = ’My dog has ’ + str ( fleacount ) + ’ fleas’ >> print fleastring My dog has 12 fleas Here, we are joining together three strings into the combined string, and printing it. The + operator, for strings, joins the three parts together, as we saw in Module 5. But this can get unwieldy. 9.5 An alternative way to build strings There is a more powerful way to build strings, using the format operator, % . (Yes, this is also the same operator that we use for remainders: 10%3 gives the value 1, because 10/3 has remainder 1. It’s being overloaded in this case to have a different meaning when its first argument is a string.) • The format operator, % lets you describe the string you want to build, and then supply the unknown parts as the other argument of the operator. • It’s easiest to show by a first example: >> fleastring = ’My dog has %d fleas’ % fleacount >> print fleastring My dog has 12 fleas 9.6 How the format operator works The format operator has two arguments: description % fields : • The first argument to the % operator gives the string that you want to build. – You can characterize features that you want to put into the string by using cues inside that string. – We used %d to show that we wanted to implant an integer in the string, between ’has’ and ’fleas’ ....
View Full Document

This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Page1 / 11

Module 9 - Module 9 File input and output Keeping it...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online