在Python中从用户获取多个input
我知道如何从python 2.5中的用户采取一个单一的input:
raw_input("enter 1st number") 这打开了一个input屏幕,并采取了第一个号码。 如果我想要第二个input,我需要重复相同的命令,并在另一个对话框中打开。 我怎样才能把两个或更多的input一起打开在同一个对话框中:
 Enter 1st number:................ enter second number:............. 
	
这样的事情呢?
 user_input = raw_input("Enter three numbers separated by commas: ") input_list = user_input.split(',') numbers = [float(x.strip()) for x in input_list] 
(你也许会想要一些error handling)
这可能是有用的:
 a,b=map(int,raw_input().split()) 
然后可以分别使用“a”和“b”。
或者如果你正在收集许多数字,使用循环
 num = [] for i in xrange(1, 10): num.append(raw_input('Enter the %s number: ')) print num 
我的第一印象是,你想循环命令提示符循环用户input内循环命令提示符。 (嵌套的用户input。)也许这不是你想要的,但我已经写了这个答案之前,我意识到这一点。 所以,如果其他人(或者甚至你)发现它有用,我会发布它。
您只需要在每个循环的级别使用input语句的嵌套循环。
例如,
 data="" while 1: data=raw_input("Command: ") if data in ("test", "experiment", "try"): data2="" while data2=="": data2=raw_input("Which test? ") if data2=="chemical": print("You chose a chemical test.") else: print("We don't have any " + data2 + " tests.") elif data=="quit": break else: pass 
您可以使用下面的内容来获取由关键字分隔的多个input
 a,b,c=raw_input("Please enter the age of 3 people in one line using commas\n").split(',') 
Python和所有其他命令式编程语言一个接一个地执行一个命令。 因此,你可以写:
 first = raw_input('Enter 1st number: ') second = raw_input('Enter second number: ') 
 然后,你可以在第first和secondvariables上进行操作。 例如,您可以将存储在其中的string转换为整数 ,并将它们相乘: 
 product = int(first) * int(second) print('The product of the two is ' + str(product)) 
在Python 2中,可以分别input多个值逗号(如jcfollower在他的解决scheme中提到的)。 但是,如果你想明确地做,你可以按照以下的方式进行。 我正在使用for循环从用户获取多个input,并通过用','分开保存在项目列表中。
 items= [x for x in raw_input("Enter your numbers comma separated: ").split(',')] print items 
你可以试试这个
 import sys for line in sys.stdin: j= int(line[0]) e= float(line[1]) t= str(line[2]) 
有关详情,请查看,
https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output#Standard_File_Objects
尝试这个:
 print ("Enter the Five Numbers with Comma") k=[x for x in input("Enter Number:").split(',')] for l in k: print (l)