Closing a loop, with enter

Asked

Viewed 487 times

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?

  • Need to leave the if direct, so it will stand still waiting for you to press the button to continue.

  • You can give me an example?

  • This is college stuff?

  • 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.

  • You need to leave the loop running while the enter is not pressed?

  • I want to give the user the option to terminate the program by pressing enter, if not press it restarts, something like

Show 1 more comment

2 answers

3


Just fixing your code it will work tbm:

while True:
    a = str(input(""))

    if a == "":
        break
  • a = str(input(""))&#xA; File "<string>", line 0&#xA; &#xA; ^&#xA;SyntaxError: unexpected EOF while parsing

  • @Gabriel here worked perfectly. Try to find the blank line.

  • 2

    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

You are not signed in. Login or sign up in order to post.