[Tutor] taking a tuple with input

Alan Gauld alan.gauld at btinternet.com
Sat Oct 26 12:58:27 CEST 2013


On 26/10/13 04:09, Sven Hennig wrote:

> So how can i get an int tuple with input?

You can't. input() reads strings. (assuming Python v3)
You need to convert the string yourself.
For simple floats you just call float() but there
isn't an equivalent conversion function for tuples.

You probably want to split the string by commas
and then convert each part to a float.

Something like

instr = input('Enter a point (x,y): ')
inList = [float(n) for n in instr.split(',')]   # check for parens too?
point = tuple(inList]

You can add extra checks or combine more lines
but something along those lines should work.

The bigget problem is likely to be getting users
to enter the correct format.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list