Wait for Input in Python

Wait for Input in Python

Rayven Esplanada Jul-18, 2021 Nov-01, 2020 Python Python Input

This tutorial shows you how to wait for key press in Python before proceeding with other operations.

Use input() to Wait for Input in Python

input() is a Python function that allows user input to be processed within the code. It temporarily halts all processes within the code therefore acts as a stopper to operations that are done.

In our case, we can use input() as a key-listener to stop processes until the user presses a certain key. In the case of using input(), user needs to press enter or return key.

Below is the example code.

def operation1(param):
  #insert code here
def operation2(param):
  #insert code here
def operation3(param):
  #insert code here
input("Press enter to start operations...")
ret = operation1("Sample Param")
print("\nOperation 1 has been executed successfully.")
input("\n Press enter to start operation 2...")
ret = operation2(ret)
print("Operation 2 has been executed successfully.")
input("\n Press enter to start final operation")
ret = operation3(ret)
print("All operations executed successfully. Returned a value of ", ret)

This way, before operations start, user needs to press Enter. Also, each subsequent operation needs user to press Enter as well, essentially adding a breather between the 3 operations.

Related Article - Python Input

  • Read User Input as Integers in Python
  • Detect Keypress in Python
  • Python Get User Input With Timeout
  • Take Input of Unspecified Length in Python
  • Simulate Keyboard Inputs in Python
  • Input Multiple Lines in Python