How do you display "Press ENTER to continue" in Python 3.4?

Asked

Viewed 10,409 times

8

I wanted to know how to do the " Press ENTER to continue", or the "Press any key to continue" in Python 3.4, without having to create a single variable to store ENTER.

  • Another thing: not thatYou have to create a variable for this - but it is wrong for you to worry about "having to create an extra variable to do X" 0- there is no noticeable cost with the creation (in the case of Python and other dynamic languages, nor the cost of declaring the variable) -- on the other hand if you program thinking about not creating variables, you will end up creating code more difficult to read and maintain.

  • jsbueno, actually it’s because I had to hit ENTER twice, and I wish I only had to press one. Then I imagined that the problem was that the two Enters were the following: One to be the variable and the other to give the result to the variable. So I thought I’d do it without the variable and I asked here.

4 answers

9


Simply do not store the input output in a variable

input("Pressione ENTER para continuar")
  • I didn’t know that input() could be used without a variable.

  • The input() is a function call like any other. You can assign the returned value to a variable or simply ignore it. You can also pass the direct value to another function: faz_coisa(input())

  • Like Mateus Souza, I didn’t know that either.

4

To demand the Enter (i.e. ignore everything the user enters until the Enter is pressed), see hugomg response. In case you accept any key (i.e. return whatever the user has typed, without waiting for the Enter) that question on Soen shows an implementation of getch in Python. One of its versions is:

def _find_getch():
    try:
        import termios
    except ImportError:
        # Non-POSIX. Return msvcrt's (Windows') getch.
        import msvcrt
        return msvcrt.getch

    # POSIX system. Create and return a getch that manipulates the tty.
    import sys, tty
    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

    return _getch

getch = _find_getch()

When using, you can ignore the return value if you want, but you need to print the initial message separately:

def anykey(msg='Pressione qualquer tecla para continuar'):
    print(msg)
    getch()

0

We will try to do this by finishing the current process and showing the message "Press any key to continue . . ." native to the system:

import subprocess
subprocess.check_call([r"C:\Windows\System32\cmd.exe", "/c PAUSE"])

will execute the command pause native to MS-DOS, showing with system-based language.

-1

To end the program just adapt the code to something similar to:

while True:

    sair = str( input( '\nPRESSIONE "ENTER" PARA SAIR ' ) )

    if not sair:

        exit()

Browser other questions tagged

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