Almost any interesting program has to get information from somewhere, and produce some sort of answers somewhere. These are called input and output . This sheet describes some of the ways Python handles input and output.
Some of the things in here require you to have done
from gasp import *
before they’ll work.
Here are some useful functions that ask the user a question and wait for an answer. They all expect that the user will hit Enter after typing the answer.
Expects yes, no, y or n, in either capitals or lowercase. It returns True when either yes or y is entered and False for no or n. A sample session in the interpreter might look like this:
>>> read_yesorno()
Yes or no? yes
True
>>> read_yesorno()
Yes or no? no
False
>>> read_yesorno()
Yes or no? What?
Please answer yes or no.
Yes or no? y
True
>>>
The main thing you need to know about output is the print statement. It can print any object:
>>> x = [1, 2, 3] # a list,
>>> y = 'zog' # a string,
>>> z = 99 # a number,
>>> f = repr # a function
>>> print x, y, z, f
[1, 2, 3] zog 99
Notice that it puts spaces between the things it prints.
If you write two print statements, one after the other, you’ll see that the second starts printing on a fresh line rather than following on from the first. If that isn’t what you want, put a comma at the end of the first print:
print 123,
print 456
This will print 123 456 on a single line.