簡體   English   中英

在 python 中從用戶輸入同一行取多個整數

[英]Taking multiple integers on the same line as input from the user in python

我知道如何在python 2.5中獲取用戶的單個輸入:

raw_input("enter 1st number")

這將打開一個輸入屏幕並輸入第一個數字。 如果我想進行第二個輸入,我需要重復相同的命令,然后在另一個對話框中打開。 我怎樣才能在打開的同一個對話框中將兩個或多個輸入放在一起,這樣:

Enter 1st number:................
enter second number:.............

這可能證明是有用的:

a,b=map(int,raw_input().split())

然后,您可以分別使用 'a' 和 'b'。

這樣的事情怎么樣?

user_input = raw_input("Enter three numbers separated by commas: ")

input_list = user_input.split(',')
numbers = [float(x.strip()) for x in input_list]

(您可能也需要一些錯誤處理)

或者,如果您要收集很多數字,請使用循環

num = []
for i in xrange(1, 10):
    num.append(raw_input('Enter the %s number: '))

print num

我的第一印象是,您想要一個循環命令提示符,並在該循環命令提示符內循環用戶輸入。 (嵌套的用戶輸入。)也許這不是您想要的,但在我意識到之前我已經寫了這個答案。 因此,我將發布它以防其他人(甚至您)發現它有用。

您只需要在每個循環級別帶有輸入語句的嵌套循環。

例如,

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
  1. a, b, c = input().split() # for space-separated inputs
  2. a, b, c = input().split(",") # for comma-separated inputs

您可以使用以下代碼讀取 Python 3.x 中的多個輸入,該代碼拆分輸入字符串並轉換為整數並打印值

user_input = input("Enter Numbers\n").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)

您可以使用以下內容來獲取由關鍵字分隔的多個輸入

a,b,c=raw_input("Please enter the age of 3 people in one line using commas\n").split(',')

最好的練習方法是使用單個襯墊,
句法:

列表(地圖(輸入類型,輸入(“輸入”)。拆分(“,”)))

采用多個整數輸入:

   list(map(int, input('Enter: ').split(',')))

在此處輸入圖片說明

采用多個浮點輸入:

list(map(float, input('Enter: ').split(',')))

在此處輸入圖片說明

采用多個字符串輸入:

list(map(str, input('Enter: ').split(',')))

在此處輸入圖片說明

List_of_input=list(map(int,input (). split ()))
print(List_of_input)

它適用於 Python3。

在 Python 2 中,您可以分別輸入多個逗號值(如 jcfollower 在他的解決方案中提到的)。 但是如果你想明確地做,你可以按照以下方式進行。 我使用 for 循環從用戶那里獲取多個輸入,並通過用 ',' 拆分將它們保留在項目列表中。

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

拆分函數將根據空格拆分輸入數據。

data = input().split()
name=data[0]
id=data[1]
marks = list(map(datatype, data[2:]))

name 將獲得第一列,id 將包含第二列,marks 將是一個列表,其中包含從第三列到最后一列的數據。

一種常見的安排是一次讀取一個字符串,直到用戶輸入一個空字符串。

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('\n') for line in lines]
#suppose if you want to enter 10 numbers using while loop.

list=[]
i=1
while i<=10:
 list.append(input('enter numbers:'))
 i+=1
print(list)
n = int(input())
for i in range(n):
    i = int(input())

如果您不想使用列表,請查看此代碼

Python 和所有其他命令式編程語言執行一個又一個命令。 因此,你可以只寫:

first  = raw_input('Enter 1st number: ')
second = raw_input('Enter second number: ')

然后,您可以對變量firstsecond egers and multiply them:例如,您可以將存儲在其中的字符串轉換為並將它們相乘:

product = int(first) * int(second)
print('The product of the two is ' + str(product))

有兩種方法可以使用:

  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.
  2. 如果您想將輸入作為列表獲取,則使用另一種方法,如下所示:

     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

嘗試這個:

print ("Enter the Five Numbers with Comma")

k=[x for x in input("Enter Number:").split(',')]

for l in k:
    print (l)

如何使輸入列表。 然后您可以使用標准列表操作。

a=list(input("Enter the numbers"))
# 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)

希望你得到你的答案:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM