Basic Input-Output

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'). When you play a video game, you are 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...'

Input can also come from a file. For instance,  when you type 'python someprog.py' the program called 'python' is getting its input from a file called someprog.py.

Output is what the program produces. For command-line programs, it's just whatever is printed on the screen. Of course for programs with graphical user interfaces, the output is much more complex.

In Python, the print statement is used for command-line output:

    print 45                              # just prints the number 45
    print 'my mother said'        # prints the words without the quotes
    interestRate= .4
    print interestRate              # prints the value of the variable interestRate, 0.4
    print interestRate+.2         # prints the evaluated expression, 0.6

User Input in Python

Python has two key functions to deal with end-user input, one called raw_input() and one called input(). When a program is running and these functions are called, the system will freeze, waiting for the end-user to enter something.

When the user enters something and hits 'enter', a string is returned from these functions. So on execution of the statement:

result = raw_input()


Python will wait for the end-user to enter something. If the user types in abc, then the variable result will have 'abc' in it.

Generally, we want to prompt the user for input. You can do this by adding a parameter to the 'raw_input()' call. If you were asking a person's name, you'd write the statement:

name=raw_input('please enter your name:')

User input and data types

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 int and float.

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 in string form from the user, and perform a computation on it, with the following:

ageString=raw_input('enter your age:')
age = int(ageString)
doubleAge = age*2
print doubleAge

This will print out twice the number that the user enters. Because such converting 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.

age = input('enter your age:')
doubleAge = age*2

The only problem with 'input' is you have to assume the user will enter what you are expecting. If they don't, your program will die. For instance, if a user entered ‘xyz’ for the input statement above, the program would print one of those beautiful Python error messages.

For sample programs, you can use the input() function when you are expecting an integer from the end-user, and raw_input when you are expecting a string. But keep in mind that for software that is to be used by real users, your program must handle everything the end-user might throw at it, and system error messages are not acceptable.

In-Class Assignment:

Your goal is to write a program that adds two numbers input by the end-user. You'll first write it in a simple manner, than you'll write it with error-checking.

1. SIMPLE: Just call the input function twice and add the two numbers you receive.

2. WITH ERROR CHECKING: Write code that prompts the end-user to enter a whole number and checks if the input is indeed a valid number.  Print an error message and continue to prompt the user for a valid number until he enters one. Hint: use raw_input to read the user's input string, then use a while loop to check that each character in the string is a digit. Once you can check that the input is indeed a whole number, use the code twice to get the two numbers and then add them together.

Recent site activity