3
I’m looking to close one while True:
pressing a key, in case simply pressing enter without writing anything.
while True:
key = input("Press enter")
if key == " ":
break
else:
#loop
How can I do it?
3
I’m looking to close one while True:
pressing a key, in case simply pressing enter without writing anything.
while True:
key = input("Press enter")
if key == " ":
break
else:
#loop
How can I do it?
3
Just fixing your code it will work tbm:
while True:
a = str(input(""))
if a == "":
break
a = str(input(""))
 File "<string>", line 0
 
 ^
SyntaxError: unexpected EOF while parsing
@Gabriel here worked perfectly. Try to find the blank line.
Just remembering that input
already returns a string, use the return with str
is redundant and expends resources for no reason.
1
To solve the problem the way it just needs to remove the space between the quotes.
In your code your if
is comparing the input with a string of a character that is the space " "
. When you press enter on an input with no character you are sending an empty string ""
, then for solution you should compare the input with an empty string ""
.
The code shall be as follows::
if a == "":
break
Browser other questions tagged python python-3.x
You are not signed in. Login or sign up in order to post.
Need to leave the if direct, so it will stand still waiting for you to press the button to continue.
– user142809
You can give me an example?
– Gabriel
This is college stuff?
– user142809
Use asyncio or use Pipes. https://docs.python.org/3/library/asyncio-subprocess.html#asyncio-subprocess here is an example. If using Pipes just use
mkfifo
on the linux terminal to create a pipe-like file, then the scheme is FIFO (first in, first out), first in, first out. Then you leave your program running reading the file with the loop and do another to write on it.– user142809
You need to leave the loop running while the enter is not pressed?
– Woss
I want to give the user the option to terminate the program by pressing enter, if not press it restarts, something like
– Gabriel