Continue a Loop Until Key Pressed Python
input() method is used in python 3 for data intake from the user. Sometimes, it requires waiting or pause the input task for a specific period of time for programming purposes. For example, if there is an infinite loop in the script that will terminate based on specific user input, then it will wait for the input from the user in each iteration of the loop. Time module contains sleep() method that can be used to wait a certain period of time in python before taking input. Python contains many other modules to terminate the script based on any key press or pause the execution of the script. How python pause for input can be applied in python script is shown in this article.
Pause the script for user input to terminate:
If you want to wait for the user to press any key before terminating the script then you can call input() method with a message at the end of the script. The following script shows how you can pause the termination of the script and wait for the user's input. The input() method will take string data and store in the variable, name. If the variable is not empty then a welcome message will print otherwise an error message will print. Next, an instruction message will print to inform the user to press any key. A termination message will print when the user will press any key.
#!/usr/bin/env python3
# Take an user input
name = input ( "What is your name? " )
# Check the input value
if (name != "" ):
# Print welcome message if the value is not empty
print ( "Hello %s, welcome to our site" %name )
else:
# Print empty message
print ( "The name can't be empty." )
# Wait for the user input to terminate the program
input ( "Press any key to terminate the program" )
# Print bye message
print ( "See you later." )
Output:
After executing the script, it waits for a string input. Here, 'Fahmida' is typed as a string value. A welcome message is printed with the value and waited for any keypress. A bye message is printed after pressing any key.
Pause the input for a certain period of time
sleep() method can be used to pause the user input for a certain period of time. In the following script, a simple addition task is given for the user. sleep() method is used here to wait for the user for 5 seconds before typing the answer. Next, if the condition is used to check the answer is correct or incorrect.
#!/usr/bin/env python3
# Import time module
import time
# Print the message
print ( "Solve the problem to prove that you are a human being." )
# Print the question
print ( "What the sum of 10 and 40? " )
# Print wait message
print ( "Waiting for five seconds for calculating ..." )
# Wait for 2 seconds
time.sleep ( 5 )
# Take input from the user
answer = input ( "Your answer: " )
# Check the answer
if ( int (answer) == 50 ):
print ( "Your answer is correct. Well done." )
else:
print ( "You have failed to prove." )
Output:
A question will print after running the script and inform the user to wait for 5 seconds to find out the answer. Here, the script is executed two times with a correct answer and an incorrect answer.
Pause the script using the input to display the instruction messages
Sometimes it is required to pause the script multiple times using input() method for different purposes. The process of providing information to the user by using multiple messages is shown in the following script. The steps to run any python script are shown here by using multiple input() method. The user has to press any key to show the next steps. The first input() method is used to start showing the message and the last input() method is used to show the termination message.
#!/usr/bin/env python3
# Print the starting message
print ( "The steps to run a python script:" )
# Wait for any keypress
input ( "Press any key to continue" )
# Wait for any keypress
input ( "Write the script in any editor." )
# Wait for any keypress
input ( "Open the terminal by pressing Alt+Ctrl+T." )
# Wait for any keypress
input ( "Type: 'python scriptname.py'." )
# Wait for any keypress
input ( "You will get your output if the script is error-free." )
# Wait for any keypress
input ( "Press any key to terminate." )
# Print the termination message
print ( "\nGood Bye." )
Output:
The following output will appear after running the script. The user has to press any key five times to complete the execution of the script.
Pause the script for the particular input value
If you want to run some scripts continuously until the user press any specific key then you have to define that script inside any infinite loop. This task is shown in this example. Here, an infinite while loop is declared and is expected to take two numbers and print the summation of those numbers in each iteration. At the end of the loop, it will wait for the user to press 'y' to continue the loop and repeat the script again.
#!/usr/bin/env python3
# Define a infinite loop
while ( True ):
# Take two integer numbers
x = int ( input ( "Enter a number: " ) )
y = int ( input ( "Enter a number: " ) )
# Add two numbers
result = x + y
# Print the summation result
print ( "The sum of %d and %d is : %d" %(x, y, result) )
# Wait for the user input to continue or terminate the loop
ans = input ( "Do you want do again? (y/n)" )
# Terminate the script if the input value is 'n'
if (ans.lower ( ) == 'n' ):
break
Output:
The script inside the while loop is executed two times here. The first time, after calculating the summation, 'y' is pressed and the script of the loop is repeated again. When the user pressed 'n' then the loop is terminated.
Conclusion:
Pause for the user input is a very common requirement of any programming language. Different purposes of pause for the input are shown in this article by using very simple python examples. I hope, this article will help the reader to know the uses of pause for the input and apply it in the script when requires.
Source: https://linuxhint.com/python_pause_user_input/
0 Response to "Continue a Loop Until Key Pressed Python"
Post a Comment