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."
Related
This question already has answers here:
How to run a script forever? [duplicate]
(8 answers)
Closed 3 years ago.
I do not know how to do code that is executing over and over again. I would like to achieve something like this:
(< stands for input, > stands for output)
message=input()
print('Write down the text.')
>Write down the text.
<qwerty
>qwerty
>Write down the text.
<asd
>asd
You can achieve this with a
while True:
// code you want to execute repeatedly here
The while loop will continue to execute until the condition becomes false (which it never will, in this case) so if/when you want to break out of the loop you'll need to have use a break statement
Do you mean like this?
while True:
message=input("Write down the text: ")
print(message)
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
Basically, I have a "raw_input" then an "if" statement and then many "elif" statements after it. If the user inputs invalid data I want the "else" to print a string, then restart the program so the user can try again. How can I do this?
Put your code in a loop:
while True:
# Code here
To exit the loop, use break, which will also quit the program.
while True:
# Code here
if running == False: # The condition for breaking is up to you, if you're using one
break
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.
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
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