which_one = input("What month (1-12)? ") months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\ 'August', 'September', 'October', 'November', 'December'] if 1 <= which_one <= 12: print "The month is",months[which_one - 1]
and a output example:
What month (1-12)? 3 The month is March
In this example the months is a list. months is defined with the lines months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\
'August', 'September', 'October', 'November', 'December'] (Note that a \
can be used to split a long line). The [
and ]
start and end the list with comma's (``,
'') separating the list items. The list is used in months[which_one - 1]
. A list consists of items that are numbered starting at 0. In other words if you wanted January you would use months[0]. Give a list a number and it will return the value that is stored at that location.
The statement if 1 <= which_one <= 12: will only be true if which_one is between one and twelve inclusive (in other words it is what you would expect if you have seen that in algebra).
Lists can be thought of as a series of boxes. For example, the boxes created by demolist = ['life',42, 'the universe', 6,'and',7] would look like this:
box number | 0 | 1 | 2 | 3 | 4 | 5 |
demolist | `life' | 42 | `the universe' | 6 | `and' | 7 |
Each box is referenced by its number so the statement demolist[0] would get 'life', demolist[1] would get 42 and so on up to demolist[5] getting 7.