簡體   English   中英

將多行輸入存儲到字符串中 (Python)

[英]Store multi-line input into a String (Python)

輸入:

359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

我的代碼:

print("Enter the array:\n")   
userInput = input().splitlines()
print(userInput)

我的問題是, userInput只接受第一行值,但似乎沒有接受第一行之后的值?

您可以使用文件對象的readlines()方法:

import sys
userInput = sys.stdin.readlines()

您可以使用生成器輕松創建一個。 這是一個這樣的實現。 請注意,您可以按空白返回或任何鍵盤中斷來跳出輸入循環

>>> def multi_input():
    try:
        while True:
            data=raw_input()
            if not data: break
            yield data
    except KeyboardInterrupt:
        return


>>> userInput = list(multi_input())
359716482
867345912
413928675
398574126

>>> userInput
['359716482', '867345912', '413928675', '398574126']
>>> 

每個input()只接受一行。 圍繞這個的策略:

  • 您可以在循環中重復調用input()直到它收到一個空行
  • 您可以在循環中重復調用input()直到用戶在類 UNIX 操作系統上執行 ctrl-D ,此時將引發 EOFError ,您可以捕獲
  • 從文本文件或其他比標准輸入更合適的來源讀取數據

你好呀,

您可以嘗試我根據您的要求制定的這種方法。 我在代碼上添加了一些注釋來解釋我想要實現的目標。 很高興有機會學習一些 Python 代碼,我是這門語言的新手,但我非常喜歡它!

def multi_input():
    try:
        #This list will hold all inputs made during the loop
        lst_words = []
        # We will store the final string results on this variable
        final_str ='' 
        #Let's ask first the user to enter the amount of fruits
        print("Please Type in your List of Fruits. \n Press << Enter >> to finish the list:")
        
        #Loop to get as many words as needed in that list
        while True:
            #Capture each word o'er here, pals!
            wrd = input()
            # If word is empty, meaning user hit ENTER, let's break this routine
            if not wrd: break
            # if not, we keep adding this input to the current list of fruits
            else:
                lst_words.append(wrd)

#What if ther user press the interruption shortcut? ctrl+D or Linus or MacOS equivalent?
    except KeyboardInterrupt:
        print("program was manually terminated by the user.")
        return
#the time has come for us to display the results on the screen
    finally:
        #If the list has at least one element, let us print it on the screen
        if(len(lst_words)>0):
            #Before printing this list, let's create the final string based on the elements of the list
            final_str = '\n'.join(lst_words)
            print('You entered the below fruits:')
            print(final_str)
        else:
            quit
#let us test this function now! Happy python coding, folks!
multi_input()
lines = []
while True:
s =input("Enter the string or press ENTER for Output: ")
if s:
    lines.append(s)
else:
    break;

print("OUTPUT: ")
for i in lines:
print (i)

Input:
359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

Output:
359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

暫無
暫無

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

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