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
We can use int() on tuples. 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.
To use them as integers you will need to convert the user input into an integer using the int() function. e.g. age=int(input("What is your age?")) This line of code would work fine as long as the user enters an integer.
List comprehension along with zip() function is used to convert the tuples to list and create a list of tuples. Python iter() function is used to iterate an element of an object at a time. The 'number' would specify the number of elements to be clubbed into a single tuple to form a list.
tuple(int(x.strip()) for x in raw_input().split(','))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With