在python中,将同一行上的多个整数作为用户输入
- 2025-02-18 09:24:00
- admin 原创
- 36
问题描述:
我知道如何从用户那里获取单个输入python 2.5
:
raw_input("enter 1st number")
这将打开一个输入屏幕并输入第一个数字。如果我想输入第二个数字,我需要重复相同的命令,然后会打开另一个对话框。如何在打开的同一个对话框中同时输入两个或多个数字,以便:
Enter 1st number:................
enter second number:.............
解决方案 1:
这可能会有用:
a,b=map(int,raw_input().split())
然后您可以分别使用“a”和“b”。
解决方案 2:
这样的事情怎么样?
user_input = raw_input("Enter three numbers separated by commas: ")
input_list = user_input.split(',')
numbers = [float(x.strip()) for x in input_list]
(您可能还需要一些错误处理)
解决方案 3:
或者如果你要收集很多数字,可以使用循环
num = []
for i in xrange(1, 10):
num.append(raw_input('Enter the %s number: '))
print num
解决方案 4:
我的第一印象是您想要一个循环命令提示符,并在该循环命令提示符内循环用户输入。(嵌套用户输入。)也许这不是您想要的,但在我意识到这一点之前,我已经写了这个答案。所以,我要发布它,以防其他人(甚至你)发现它有用。
您只需要在每个循环级别上使用输入语句进行嵌套循环。
例如,
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
解决方案 5:
您可以使用以下代码在 Python 3.x 中读取多个输入,该代码拆分输入字符串并转换为整数,然后打印值
user_input = input("Enter Numbers
").split(',')
#strip is used to remove the white space. Not mandatory
all_numbers = [int(x.strip()) for x in user_input]
for i in all_numbers:
print(i)
解决方案 6:
最好的练习方法是使用一行代码,
语法:
列表(map(inputType,input("Enter")。split(",")))
接受多个整数输入:
list(map(int, input('Enter: ').split(',')))
获取多个浮点型输入:
list(map(float, input('Enter: ').split(',')))
获取多个字符串输入:
list(map(str, input('Enter: ').split(',')))
解决方案 7:
a, b, c = input().split() # for space-separated inputs
a, b, c = input().split(",") # for comma-separated inputs
解决方案 8:
您可以使用下面的方法获取由关键字分隔的多个输入
a,b,c=raw_input("Please enter the age of 3 people in one line using commas
").split(',')
解决方案 9:
List_of_input=list(map(int,input (). split ()))
print(List_of_input)
它适用于 Python3。
解决方案 10:
Python 和所有其他命令式编程语言都会依次执行命令。因此,您只需编写:
first = raw_input('Enter 1st number: ')
second = raw_input('Enter second number: ')
然后,就可以对变量first
和进行操作了second
。例如,你可以将其中存储的字符串转换为整数,然后将它们相乘:
product = int(first) * int(second)
print('The product of the two is ' + str(product))
解决方案 11:
在 Python 2 中,您可以用逗号分开输入多个值(正如 jcfollower 在他的解决方案中提到的那样)。但是如果您想明确地执行此操作,则可以按以下方式进行。我使用 for 循环从用户那里获取多个输入,并通过用“,”分隔将它们保存在项目列表中。
items= [x for x in raw_input("Enter your numbers comma separated: ").split(',')]
print items
解决方案 12:
你可以尝试一下。
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
解决方案 13:
Split 函数将根据空格分割输入数据。
data = input().split()
name=data[0]
id=data[1]
marks = list(map(datatype, data[2:]))
name 将占据第一列,id 将包含第二列,marks 将是一个包含从第三列到最后一列的数据的列表。
解决方案 14:
一种常见的安排是一次读取一个字符串,直到用户输入一个空字符串。
strings = []
# endless loop, exit condition within
while True:
inputstr = input('Enter another string, or nothing to quit: ')
if inputstr:
strings.append(inputstr)
else:
break
这是 Python 3 代码;对于 Python 2,您应该使用raw_input
而不是input
。
另一种常见的安排是从文件中读取字符串,每行一个。这对用户来说更方便,因为他们可以返回并修复文件中的拼写错误并重新运行脚本,而对于需要交互式输入的工具,他们无法做到这一点(除非您花费大量时间在脚本中构建编辑器!)
with open(filename) as lines:
strings = [line.rstrip('
') for line in lines]
解决方案 15:
n = int(input())
for i in range(n):
i = int(input())
如果你不想使用列表,请查看此代码
解决方案 16:
有两种方法可以使用:
此方法使用列表推导,如下所示:
x, y = [int(x) for x in input("Enter two numbers: ").split()] # This program takes inputs, converts them into integer and splits them and you need to provide 2 inputs using space as space is default separator for split.
x, y = [int(x) for x in input("Enter two numbers: ").split(",")] # This one is used when you want to input number using comma.
如果您想以列表形式获取输入,则可以使用另一种方法,如下所示:
x, y = list(map(int, input("Enter the numbers: ").split())) # The inputs are converted/mapped into integers using map function and type-casted into a list
解决方案 17:
尝试一下:
print ("Enter the Five Numbers with Comma")
k=[x for x in input("Enter Number:").split(',')]
for l in k:
print (l)
解决方案 18:
如何将输入设为列表。然后您可以使用标准列表操作。
a=list(input("Enter the numbers"))
解决方案 19:
# the more input you want to add variable accordingly
x,y,z=input("enter the numbers: ").split( )
#for printing
print("value of x: ",x)
print("value of y: ",y)
print("value of z: ",z)
#for multiple inputs
#using list, map
#split seperates values by ( )single space in this case
x=list(map(int,input("enter the numbers: ").split( )))
#we will get list of our desired elements
print("print list: ",x)
希望你得到答案:)
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)