How to take multiple inputs from a user in Python [duplicate] - python

This question already has answers here:
How to read user input until EOF?
(4 answers)
How do I read from stdin?
(25 answers)
Closed 17 days ago.
I'm trying to create a program where python will take multiple inputs until the user enters "END". Each input must be on a different line. I'm still new and learning python/programming in general so is there any way to do this? and how would I turn the inputs into a list?
What a demo might look like -
What are your hobbies?
painting
hiking
drawing
END

You can read input with the input() function. You want to read till 'END' is entered by the user. So, read the input and check whether it is 'END'. If not append it to the list else exit the loop.
In codes you can do like this.
inputs = [] # list to store the inputs
while True: # looping forever
data = input('Enter the data : ') # read the data from user to variable data
if data == 'END': # if END is read then exit the loop
break
else:
inputs.append(data) # otherwise, append the input to the list
print(inputs) # display the list.
Hope this helps.!!

Related

Python - The "input()" function doesn't get executed [duplicate]

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
Closed last year.
I wrote this code in Python3:
for _ in range(2):
inputs = input()
print(inputs)
It works fine when I provide the input values one by one.
It also works when the input values are copied and pasted as in Image 1 and the Image 2 is printed in the terminal.
However, when the input is copied and pasted as in Image 3, it just doesn't print the last line and expects the user to press enter (or to add more characters and then press enter) to finish the code: Image 4
What is going on?
Why is this happening and how can I solve it when some other program or a user gives the input that way?
There is no new line after the second input please provide new line (by hitting enter) .
After insert 1 10 there is no new line and input method is waiting for user to provide a new line so it will stop reading and return string as output.

Is there an easy way to have a multiline input in Python 3? [duplicate]

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
How to get multiline input from the user [duplicate]
(5 answers)
Closed 2 years ago.
If I have a large multiline input and I run this line:
story = input()
it reads it as two inputs and my program doesn't process it correctly. I found a fix on stackoverflow that I modified to look like this:
story = []
while True:
try:
line = input()
except EOFError:
break
story.append(line)
story = ' '.join(story)
But I would need to use ctrl+D to induce an EOFError to stop the input. Is there a more intuitive way on Python to copy paste a large multiline input?

Having problems how to use an if statement to determine if something is in a list or not. Python [duplicate]

This question already has answers here:
Python check if Items are in list
(5 answers)
Is there a short contains function for lists?
(6 answers)
Closed 5 years ago.
I am writing a program that takes a blank input and does various things with it. The input "show me contact _____" (With the ____ being filled in with somebody's name). The names are stored in a list and I need it to be able to check if the name is in the list, and if it is, print the contact information. I figured an if statement would be the best way to do it, but I cannot figure out how.
Edit: I have tried using in, but my problem is that the input contains "show me contact _____" and not just the name. This causes this:
names = ['john doe','john doe','john doe']
user_input = input('>')
if user_input in names:
print(email)
to not work.
You want
if user_input in names:
When used with a list, in checks for membership. When used with a string, it will check for substrings.

Multi-line user input [duplicate]

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
Closed 7 years ago.
I want to be able to accept multi-line user input. The problem is that with my program the user can't just use Shift Enter to do it.
I want them to be able to paste in several paragraphs for translation and when it detects a new line it automatically takes it as an enter and cuts off the input. I am using raw_input, not input.
Does anyone know a way to overcome this, so it can accept pasted text with multiple lines without assuming it is the end of the input?
You can repeatedly ask for raw_input() in a loop and concatenate the lines until you hit some input that signalizes the end. A very common one would be an empty line:
allLines = []
print('Insert the text:')
while True:
line = raw_input('')
if line == '':
break
allLines.append(line)
fullInput = '\n'.join(allLines)
print('You entered this:')
print(fullInput)

Stop input function in python [duplicate]

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 8 years ago.
I would like to stop the user to type in too many characters in the input function so it just stops the input() when too many characters are put in but the characters previously typed in stay. In this case I wouln't want to check if this is after the user has pressed enter but I would like to interrupt the function. Is this somehow possible?
Might be possible with a loop and msvcrt. Here is an example:
while len(string) < 10:
#Whatever length you want instead of 10
string += msvcrt.getch()
I don't believe this is possible, but if you're concerned about a maximum length (for a database value or something similar) you can use slicing to address excessively long entries:
maxlen = 256
userval = input("Enter value (max 256):")[:maxlen]

Categories