I/O is short for input-output.
Input usually means the data entered by the end-user of the program. When you use Microsoft Word, you are playing the role of the end-user (sometimes shortened to just plain 'user'). The player of a video game is its 'end-user'.
When programming, we often switch between playing the role of programmer and end-user. We're the end-user when we are testing whether the program works. We usually call the program itself the system, as in 'when the end-user enters a number, the system responds by calculating a result and displaying it.'
The input of a program can also come from a file, e.g., banking software might run through all the data in its files, adding interest to every account.
Output is what the program produces. For command-line programs, its just whatever is printed on the screen. Of course for programs with graphical user interfaces, its much more complex.
Python has two key functions to deal with end-user input, one called raw_input() and one called input(). When you call these functions, the system will wait for the end-user to enter something. The system return the user's entry as a string result from the function.
If you execute the statement:
>>> result = raw_input()
Python will wait for you (the end-user) to enter something. Enter some text and hit return. Then type
>>> result
This should print whatever the end-user typed.
Generally, we want to prompt the user for input. You can do this by adding a parameter to the 'raw_input()' call:
>>> x = raw_input('please enter a string:')
please enter a string: hello
>>> x
'hello'
Generally, we use input functions from a program, not the interactive interpreter. Open an IDLE window and create a file with the following line:
result = raw_input('please enter a string:)'
print result
save the file as io.py, then run the program.
The function raw-input() always returns a string. So if the user enters 7, Python interprets it as the string '7'.
Sometimes this is fine, but often we want to perform computations on numbers, and as far as the computer is concerned, '7' is a symbol and not a number.
If you want to 'interpret' input as an integer, you need to convert it. Python provides some type conversion functions to do so, such as:
x=int('7') # puts the integer 7 into x.
f=float('7.7') #puts the floating point number 7.7 into f.
Using a type converter, one could get an integer from the user, and peform a computation on it, with the following:
>> s=raw_input('enter a number:')
enter a number: 6
>> x = int(s)
>> y = x+3
>> y
9
Because this is a bit laborious, Python provides the 'input()' function, which converts things automatically. 'input()' basically looks at what the user enters, and automatically determines the correct type.
>> x = input('enter a number:')
enter a number: 4
>> x
4
The only problem with 'input' is you have to assume the user will enter what you are expecting. If they don't, you are in trouble. For instance,suppose as a programmer I'm expecting an integer. What happens on the following:
>> x = input('enter a number:')
enter a number: abce
>> y = x+3
For now, just use the input() function when you are expecting an integer from the end-user, and raw_input when you are expecting a string. Later, will talk about validating user input in detail.
In-Class Problem
1. Write a program that asks the end-user for an account balance and an interest rate, and then displays what the balance will be after one year.