How to get multi-line input from user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How to get multi-line input from user?

How to get multi-line input from user? Sample input: 2 E W Output: 2 E W Sample input: 7 U Oos -( * J Output: 7 U Oos -( * J

13th Sep 2021, 12:38 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
19 Answers
+ 8
#MD. Ferdous Ibne Abu Bakar please try this: for x in range(100): try: print(input()) except: pass
13th Sep 2021, 1:28 PM
SAN
SAN - avatar
+ 10
MD. Ferdous Ibne Abu Bakar You can read multi-line input a number of ways. 1st Method: inputlist = [] while True: try: line = input() except EOFError: break inputlist.append(line) 2nd Method import sys inputlist = sys.stdin.readlines() print(inputlist) This will take multi-line input however you need to terminate the input (ctrl+d or ctrl+z). You could change the while loop in 1st method to a range if the number of lines doesn’t change. The second method will not work in Sololearns code playground.
13th Sep 2021, 1:55 PM
DavX
DavX - avatar
+ 7
MD. Ferdous Ibne Abu Bakar , getting multilind input in python: 1 > defining number N as the first input, then read data inputs in a loop N times 2 > doing input until "end-indicator" is input, e.g: '**' or empty input in a loop 3 > doing input in a single line by using a delimiter and split the input ▪︎but before going in depth, it is necessary to know why the inputs have to be in different lines. may be there is no necessity for doing so! please tell us a bit more about your intent. thanks!
13th Sep 2021, 3:50 PM
Lothar
Lothar - avatar
+ 3
DavX Imho specifying the error name is much better than just plain except since the error is obvious
13th Sep 2021, 2:54 PM
Tim
Tim - avatar
+ 3
Use gets() or else if u want in C not in Py u can also use 'Edit set conversion method', use ; to terminate the string at the end ex : %[^;]
14th Sep 2021, 9:26 AM
GURURAJ KL
GURURAJ KL - avatar
+ 2
Are you doing a challenge on Sololearn?
13th Sep 2021, 12:40 PM
Tim
Tim - avatar
+ 2
The best way is to create a list and append each input to that list by the append method : listname.append(input) And for the Unlimited inputs you need While True and for the limited input you need( for i in range(argument) )
13th Sep 2021, 5:33 PM
Amirreza
Amirreza - avatar
+ 2
# Python program showing how to # multiple input using split # taking two inputs at a time x, y input("Enter a two value: ").split() print("Number of boys: ", x) print("Number of girls: ", y) print() # taking three inputs at a time x, y, z=input("Enter a three value: ").split() print("Total number of students: ", x) print("Number of boys is : ", y) print("Number of girls is : ", z) print () # taking two inputs at a time a, b input ("Enter a two value: ").split() print("First number is {} and second number is {}".format(a, b)) print () # taking multiple inputs at a time # and type casting using list() function x = list(map(int, input("Enter a multiple value: ").split())) print("List of students: ", x)
14th Sep 2021, 7:16 AM
T.M.Sarabesh Kannaa
T.M.Sarabesh Kannaa - avatar
+ 2
MD. Ferdous Ibne Abu Bakar gets() isn’t part of Python, its C. It would work on Sololearn code if you were using C: char str[20]; gets(str); However the question was tagged as Python! gets() is considered bad practice, as can cause buffer overrun. It makes me chuckle how many users come on here and: A) Don’t read the question. B) Don’t read the comments. Fair enough if you have something different to give, but posting the same solutions and half explained answers again and again is tiresome!
14th Sep 2021, 11:03 AM
DavX
DavX - avatar
+ 1
SAN It's working 😍
13th Sep 2021, 1:44 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
13th Sep 2021, 1:48 PM
SAN
SAN - avatar
+ 1
Guys i borrow your help
13th Sep 2021, 7:24 PM
Yese Ponsian
+ 1
I am not sure
14th Sep 2021, 3:28 PM
Odonghanro Faith
Odonghanro Faith - avatar
0
Nick, No
13th Sep 2021, 12:40 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
✩✮★✮✩ Output: EOF Error
13th Sep 2021, 1:43 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
Lothar welcome 🙂
13th Sep 2021, 4:03 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
Amirreza Hashemi thanks, will try it...
13th Sep 2021, 5:49 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
GURURAJ KL gets() doesn’t work in Sololearn... By the way thanks 🙂
14th Sep 2021, 10:21 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
14th Sep 2021, 3:59 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar