开发者

How to input an integer tuple from user?

Presently I am doing this

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

print 'Enter target'
target = tuple(sys.std开发者_开发问答in.readline())

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


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 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...


t= tuple([eval(x) for x in input("enter the values: ").split(',')])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜