Stop Python input when a specific character is entered [duplicate] - python

This question already has answers here:
How to read a single character from the user?
(26 answers)
Python sys.stdin.read(1) in a while(True) loop consistently executes 1 time getting input and multiple times not getting input
(2 answers)
Closed 6 years ago.
I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:
i = 0
array = []
while (i < 10):
array.append(raw_input("Prompt: "))
i = i + 1
However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.
I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.
Do you have any idea? Thanks in advance.

It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.
Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user
Then you need to process each character in a for loop.

Related

Adding error check gets script stuck in infinate loop (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int

how to take multi line string input from user in python,without breaking the input [duplicate]

This question already has answers here:
How to get multiline input from the user [duplicate]
(5 answers)
Closed 3 years ago.
we face the problem while taking multi-line input for strings in python.
s=str(input("enter string"))
when i enter the input
for suppose let us assume that i entered my input as:
hello friends!!
good morning
when i try to print this i get only 'hello friends!!'
is there any other code to take until i press a special symbol to tell to compiler that it is EOF of my input
like in c we have code like scanf("%[^~]",str);
it takes the input until we press "~" symbol.
like this do we have any special operations in python.
expected output of above program is
hello friends!! good morning
This has been asked here many times. Please use the search function or click here: How to get multiline input from user
tldr:
import sys
msg = sys.stdin.readlines()

Read python shell [duplicate]

This question already has answers here:
Taking input from sys.stdin, non-blocking
(7 answers)
Closed 6 years ago.
Is there a way to check if there is anything entered in the shell without using raw_input?
For example instead of having something like question = raw_input("Enter Q to quit") I just want to read if anything is entered into the shell, "Q" and quit.
The reason I need this is because I don't want to block my program from executing with the raw_input. I need it to periodically check at any given point if "Q" is entered at all.
Something like this might work
import sys
data_input = sys.stdin.readlines()
print "Counted", len(data_input), "lines."

Repeat a set of code, until something is achieved - Python [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I have a program that gets a string from a user (raw_input) and sees if the length of it is greater than 8, for example. If it isn't it starts again, otherwise the program ends.
I'm using Python 2.7.8 and this is the code I'm using so far, but it doesn't work...
password = raw_input("Type in a password: ")
if password.len() > 8 :
print "Successful!"
else :
goto(1)
Can anyone solve this problem?
You need to learn the basics. You should start with a Python tutorial.
Here's the official Python tutorial from python.org; this section introduces the while statement, which you can use to make a loop.
https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming

Raw_input in cmd Python [duplicate]

This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 9 years ago.
I have problem with raw_input command. I use it in program and run the program in cmd. Then it asks input but when I paste there text which has empty lines in it, then python takes it automatically as enter command. I want it to understand that it's not enter command but just new line. How could I do this?
To do that, you can't use raw_input function, because this function finish reading input after first new line character. Instead you can use sys.stdin:
>>>import sys
>>>input = sys.stdin.read()
hi
this text has
new lines
Ctrl^D
>>>print input
hi
this text has
new lines
To finish reading input you'll have to send EOF char, which in Linux is Ctrl+D and in Windows is Ctrl+Z
Hope this helps!
as per python documentation, raw_input() is designed to have \n as the key that ends the input, and it has not been designed for another way of interacting.
If you want to use another way of using the input you can try using fileinput() or sys.stdin.read() and iterate over the lines or the characters and create the behavior you're looking for.
Have a look at the answers of questions such as that one

Categories

Resources