Userinput

Python has two main functions for obtaining user input: the input() and raw_input() function. These can be combined with loops to read in user input that takes a variety of forms.

Both the input() and raw_input() functions are documented here.

The raw_input() function

The raw_input() function reads a string from the user. This string is gathered when the user presses the return key, which technically adds a newline character to the end of the string: raw_input() strips off this newline character.

Some examples using the python interpreter:

>>> userin = raw_input()
34
>>> print userin
>>> userin = raw_input()
Hello
>>> print "you said", userin
you said Hello

Using a prompt

The raw_input() function also allows you to add a prompt for the user:

>>> userin = raw_input("Type your name: ")
Type your name: Peter
>>> print "hello", userin
hello Peter

Mistakes with raw_input()

The main thing to bear in mind with raw_input is that everything it reads in is a string. The error message in this next example tells us that you can't add a number to a string:

>>> userin = raw_input("Type a number: ")
Type a number: 4
>>> print 5 + userin
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'

To use the input as anything else, you'll need to convert it into a different data type:

>>> userin = raw_input("Type a number: ")
Type a number: 4
>>> print 5 + int(userin)
9

The input() function

The input() function assumes that what you type is a valid Python expression, for example a plain integer:

>>> userin = input("Type a number: ")
Type a number: 3
>>> print userin + 4
7

You can even use this to take lists as input:

>>> userin = input("enter a list: ")
enter a list: [3,4,5]
>>> print userin[1]
4

However, this will break if you just type text:

>>> userin = input("Don't just type a string!: ")
Don't just type a string!: Peter
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'Peter' is not defined

Using user input with loops

The input() and raw_input() functions can be used to gather successive user inputs by using a loop. The following example keeps gathering numbers until the user inputs a 0:

userin = input("type a number: ")
while userin != 0:
    print "user has inputted:", userin
    userin = input("type a number: ")

The following shows the result of interacting with this code:

type a number: 4
user has inputted: 4
type a number: 5
user has inputted: 5
type a number: 7
user has inputted: 7
type a number: 8
user has inputted: 8
type a number: 9
user has inputted: 9
type a number: 23
user has inputted: 23
type a number: 0

Reading items from the user into a list

The following program reads in a succession of user inputted strings and stores each one in a list:

inputlist = []
userin = raw_input("type: ")
while userin != "":
    inputlist = inputlist + [userin]
    userin = raw_input("type: ")

We can reassemble what the user has typed with the following loop:

for item in inputlist:
    print item
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License