如何通过在PYTHON(raw_input())中输入空白字符来结束输入?

时间:2013-05-15 05:58:17

标签: python

如:输入两个整数a和b,用中间的空格分隔。

2 个答案:

答案 0 :(得分:3)

>>> s = raw_input('Input two integer a and b,separated by a space: ')
Input two integer a and b,separated by a space: 5 9
>>> [int(n) for n in s.split()]
[5, 9]

答案 1 :(得分:3)

使用str.split()在空格处拆分字符串,然后您可以使用maplist comprehension将数字转换为整数:

>>> n1,n2 = map(int,raw_input().split())
100 20
>>> n1
100
>>> n2
20

>>> n1,n2 = [int(x) for x in raw_input().split()]
123 43
>>> n1
123
>>> n2
43