python用空格隔开输入,接受多个用python中的空格分隔的用户输入,并将它们附加到列。。。 | 您所在的位置:网站首页 › python输入空格隔开 › python用空格隔开输入,接受多个用python中的空格分隔的用户输入,并将它们附加到列。。。 |
python ⽤空格隔开输⼊ , 接受多个⽤ python 中的空格分隔的⽤ 户输⼊,并将它们附加到列。。。 How can I accept multiple user inputs separated by a space? I don't know the number of inputs, but I do know they are all ints. Here's some example inputs: 13213 412 8143 12 312 1321 142 9421 9 29 319 I know can do this if I know the number of inputs beforehand, but I'm having trouble making this generic. I could just ask the user to input how many groups of ints he will input: inputs = int(raw_input("Enter number of raw inputs ")) num = [] for i in xrange(1, inputs): num.append(raw_input('Enter the %s number: ')) But I am looking for a more elegant solution that doesn't require asking the user 2 questions. 解决⽅案s = raw_input("Please enter your numbers: ") mynums = [int(i) for i in s.split()] # OR mynums = map(int, s.split()) |
CopyRight 2018-2019 实验室设备网 版权所有 |