python怎么区别多个input按钮,在Python中从用户获取多个input

这样的事情呢?

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])

有关详情,请查看,

尝试这个:

print ("Enter the Five Numbers with Comma") k=[x for x in input("Enter Number:").split(',')] for l in k: print (l)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值