How to get 4 numbers from the user in one line for easycomparision?

George Sakkis gsakkis at rutgers.edu
Sun Jul 3 11:25:49 EDT 2005


"Alan G" <alan.gauld at freenet.co.uk> wrote:

> > I .... Just need to figure out how to get 4 numbers from
> > the player on one line for easy comparison,
>
> Unless there is a set of batteries somewhere that I don't know
> about I think you have to rely on reading the line as a string
> and then splitting it.
>
> line = raw_input('Type 4 numbers separated by spaces: ')
> numbers = [int(n) for n in line.split(' ')]

Or if you want to assign these 4 numbers to separate variables, you can
use tuple unpacking:

a,b,c,d = map(int, raw_input('Type 4 numbers separated '
                             'by spaces:').split())

If you want to do error handling later, you may need to split this line
into two steps so that you can distinguish between invalid values and
wrong count of values (less or more than 4).

George




More information about the Python-list mailing list