7

Input:

359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

my code :

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

my problem here is that, userInput only takes in the first line value but it doesn't seem to take in values after the first line?

6 Answers 6

20

You can use readlines() method of file objects:

import sys
userInput = sys.stdin.readlines()
Sign up to request clarification or add additional context in comments.

3 Comments

Nice. For the record: this will require doing ctrl-D to end input.
Keep in mind that this won't work on the python IDLE in case the OP is using that.
Use Ctrl+z to end input for windows
9

You can easily create one, using generators. Here is one such implementation. Note you can either press a blank return or any Keyboard Interrupt to break out of the inputloop

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

Comments

2

Each input() will only accept a single line. Strategies around this:

  • You can repeatedly call input() in a loop until it receives a blank line
  • You can repeatedly call input() in a loop until the user does ctrl-D on a UNIX-like OS, at which point EOFError will be raised which you can catch
  • Read the data from a text file or other more appropriate source than stdin

2 Comments

And switch to raw_input() instead
Using print as a function, so probably Py3.
0

Hi there,

You could try this approach I made based on your request. I put some comments on the code to explain what I was trying to achieve. Glad to have the opportunity to get into some python code, I am new to this language but I totally love it!

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

Comments

0

You can also try using python file handling:

userInput = []
with open("example.txt", "r") as f :
    for x in f :
        userInput.append(x)

However, in this case you need to provide your input as a text file (example.txt).

Comments

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

2 Comments

Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
All the input can be given at once by pasting directly to get the output shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.