在python中,将同一行上的多个整数作为用户输入

2025-02-18 09:24:00
admin
原创
34
摘要:问题描述:我知道如何从用户那里获取单个输入python 2.5:raw_input("enter 1st number") 这将打开一个输入屏幕并输入第一个数字。如果我想输入第二个数字,我需要重复相同的命令,然后会打开另一个对话框。如何在打开的同一个对话框中同时输入两个或多个数字,以便:E...

问题描述:

我知道如何从用户那里获取单个输入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:

  1. a, b, c = input().split() # for space-separated inputs

  2. 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:

有两种方法可以使用:

  1. 此方法使用列表推导,如下所示:

 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.
  1. 如果您想以列表形式获取输入,则可以使用另一种方法,如下所示:

 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)

希望你得到答案:)

相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1325  
  IPD(Integrated Product Development)流程作为一种先进的产品开发管理模式,在众多企业中得到了广泛应用。它涵盖了从产品概念产生到产品退市的整个生命周期,通过整合跨部门团队、优化流程等方式,显著提升产品开发的效率和质量,进而为项目的成功奠定坚实基础。深入探究IPD流程的五个阶段与项目成功之间...
IPD流程分为几个阶段   4  
  华为作为全球知名的科技企业,其成功背后的管理体系备受关注。IPD(集成产品开发)流程作为华为核心的产品开发管理模式,其中的创新管理与实践更是蕴含着丰富的经验和深刻的智慧,对众多企业具有重要的借鉴意义。IPD流程的核心架构IPD流程旨在打破部门墙,实现跨部门的高效协作,将产品开发视为一个整体的流程。它涵盖了从市场需求分析...
华为IPD是什么   3  
  IPD(Integrated Product Development)研发管理体系作为一种先进的产品开发模式,在众多企业的发展历程中发挥了至关重要的作用。它不仅仅是一套流程,更是一种理念,一种能够全方位提升企业竞争力,推动企业持续发展的有效工具。深入探究IPD研发管理体系如何助力企业持续发展,对于众多渴望在市场中立足并...
IPD管理流程   3  
  IPD(Integrated Product Development)流程管理旨在通过整合产品开发流程、团队和资源,实现产品的快速、高质量交付。在这一过程中,有效降低成本是企业提升竞争力的关键。通过优化IPD流程管理中的各个环节,可以在不牺牲产品质量和性能的前提下,实现成本的显著降低,为企业创造更大的价值。优化产品规划...
IPD流程分为几个阶段   4  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用