Suspending execution until an event occurs is a very common requirement in message-based programs. In the past, having functions like getch in C/C++ was almost necessary for the code to keep the console alive to display the output. Now these compiler flaws have been fixed and these functions now have a specific purpose and are used for a specific use case. In this article, you will learn how to make a Python script wait for a key press. The following methods are analyzed:
- Use input function
- Use system function
Wait for a key press using the input function
This method is native and does not require any external methods or standard libraries. For this purpose you can use the input function provided by the standard Python distribution. However, the function is limited to the Enter key only, i.e. H. Press Enter to return. The following example shows how this works:
Example:
Now the code after the execution, interrupts program execution and waits for the Enter key to be pressed. When another key is pressed, the input function registers it. Only by pressing the Enter key does the function return with the data entered. If another key is pressed, such as alphabet, number, symbol, etc., the function does not return. The following method to stop program execution solves this problem.
Python3
# The argument to the function may be any descriptive text
input("Press the Enter key to continue: ")
Wait for a key to be pressed using the system function
The system function within the system library operation calls the operating system system shell for executing commands. Most operating systems come with a command that stops execution and makes the script wait for user input. This command for the Windows operating system is:
You May Also Like: What are the decorators in class methods in Python?
Answered 12 months ago
Wellington Importadora
Suspending execution until an event occurs is a very common requirement in message-based programs. In the past, having functions like getch in C/C++ was almost necessary for the code to keep the console alive to display the output. Now these compiler flaws have been fixed and these functions now have a specific purpose and are used for a specific use case. In this article, you will learn how to make a Python script wait for a key press. The following methods are analyzed:
Wait for a key press using the input function
This method is native and does not require any external methods or standard libraries. For this purpose you can use the input function provided by the standard Python distribution. However, the function is limited to the Enter key only, i.e. H. Press Enter to return. The following example shows how this works:
Example:
Now the code after the execution, interrupts program execution and waits for the Enter key to be pressed. When another key is pressed, the input function registers it. Only by pressing the Enter key does the function return with the data entered. If another key is pressed, such as alphabet, number, symbol, etc., the function does not return. The following method to stop program execution solves this problem.
Python3
Wait for a key to be pressed using the system function
The system function within the system library operation calls the operating system system shell for executing commands. Most operating systems come with a command that stops execution and makes the script wait for user input. This command for the Windows operating system is:
You May Also Like: What are the decorators in class methods in Python?