+ 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
19 Answers
+ 8
#MD. Ferdous Ibne Abu Bakar please try this:
for x in range(100):
try: print(input())
except: pass
+ 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.
+ 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!
+ 3
DavX
Imho specifying the error name is much better than just plain except since the error is obvious
+ 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 : %[^;]
+ 2
Are you doing a challenge on Sololearn?
+ 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) )
+ 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)
+ 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!
+ 1
SAN It's working 😍
+ 1
MD. Ferdous Ibne Abu Bakar welcome
+ 1
Guys i borrow your help
+ 1
I am not sure
0
Nick, No
0
✩✮★✮✩
Output: EOF Error
0
Lothar welcome 🙂
0
Amirreza Hashemi thanks, will try it...
0
GURURAJ KL gets() doesn’t work in Sololearn... By the way thanks 🙂
0
SAN i used your code here↓ 🙂
https://code.sololearn.com/cGqplw8VAOpe/?ref=app