tags:

views:

68

answers:

3

Presently I am doing this

print 'Enter source'
source = tuple(sys.stdin.readline())

print 'Enter target'
target = tuple(sys.stdin.readline())

but source and target become string tuples in this case with a \n at the end

+5  A: 
tuple(int(x.strip()) for x in raw_input().split(','))
Ignacio Vazquez-Abrams
How should the user give input now.I tried 4,0...it doesn't work
Bruce
Works fine here:`$ python -c "print tuple(int(x.strip()) for x in raw_input().split(','))"``4,0``(4, 0)`
Ignacio Vazquez-Abrams
I get the output-Enter source0,4Enter target3,2('0', '4')('3', '2')
Bruce
Ah, I modified my answer slightly after. Compare what you have with what I wrote.
Ignacio Vazquez-Abrams
Works like a charm
Bruce
`sys.stdin.readline()` does have the advantage of working across python2 and python3. In python3 `raw_input` is renamed to `input`
gnibbler
+1  A: 

If you still want the user to be prompted twice etc.

print 'Enter source'
source = sys.stdin.readline().strip()  #strip removes the \n

print 'Enter target'
target = sys.stdin.readline().strip()

myTuple = tuple([int(source), int(target)])

This is probably less pythonic, but more didactic...

mjv
source and target are themselves tuples. We can use int() on tuples.
Bruce
Sorry, the desired output is unclear. At any rate, the string.strip() and the int() should allow you to get exactly what you need, be it two tuples of a single integer each, or one tuple with the source and target values.
mjv
A: 

Turns out that int does a pretty good job of stripping whitespace, so there is no need to use strip

tuple(map(int,raw_input().split(',')))

For example:

>>> tuple(map(int,"3,4".split(',')))
(3, 4)
>>> tuple(map(int," 1 , 2 ".split(',')))
(1, 2)
gnibbler