Department of Mathematics, Computer Science, and
                Statistics
St.
                Lawrence University
Zonohedron
 

What is this object?

...

Links
Overview
Schedule
Grading Info
Class Notes

 

CS 140: Introduction to Computer Programming


Input and Output

There are quite a few ways that a program can receive information from the "outside world" while executing.  The first method that comes to mind is probably to have the user type something on the keyboard, which is what we will consider below.  But a program could also detect mouse clicks and read data from a file, among other possibilities.

There are two input commands: raw_input and input.  We use the first one to prompt the user to enter a string (such as a name, word, or zip code) while the second is used to ask the user to enter a number (either an integer or a float).  Fortunately, the syntax for these two commands is very similar.  To obtain a string, type

last_name = raw_input("Please enter your last name:")

When encountering this command, the program will print the statement Please enter your last name: on the screen, then record whatever is typed at the cursor (including lower and upper case letters, spaces, and digits) until the user hits the [RETURN] or [ENTER] keys.  This string of keystrokes is then called last_name for later reference.  It is possible to use the command without assigning a variable name, as in

raw_input("Press the [RETURN] key to continue.")

In this case it doesn't matter what is typed, because the program won't remember anyway, since the input is not given a name.

The same process can be used to get a number from the user, by typing

age = input("Enter your current age:")

Python will treat age as an integer if the number typed does not have a decimal point, but will designate it as a float if it does contain a decimal point.  In other words, Python is bright enough to figure out what data type to assign age based on the user input.  By the way, you will get an error message (and the program will halt) if the user does not type a number at an input prompt!  For instance, this would occur above if the user typed eight or just hit the [RETURN] key.

Now let's take a quick look at printing text and numbers to the screen.  The print command is used to display any data type.  One can even combine several different types on a single line using commas to separate the various items to be printed.  For instance,

total = 8
color = "purple"
print "I bought", total, color, "shirts for the semester."


would result in

I bought 8 purple shirts for the semester.

Several print statements in a row will display items on separate rows, unless the print statement is followed by a comma.  Thus the code

print "Once upon",
print "a midnight clear,"
print "A jolly fellow",
print "did appear."

would give the following output:

Once upon a midnight clear,
A jolly fellow did appear.

Note that commas inside quotes are simply printed, while the ones outside quotes are used to separate items and prevent carriage returns.