Skip to content Skip to sidebar Skip to footer

Python Closes at End of Windows q python Input to Run Again

input() method is used in python 3 for information intake from the user. Sometimes, it requires waiting or suspension the input job for a specific period of fourth dimension 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 expect for the input from the user in each iteration of the loop. Time module contains sleep() method that can be used to expect a certain period of time in python earlier 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 suspension for input tin can be applied in python script is shown in this commodity.

Pause the script for user input to terminate:

If yous want to wait for the user to press any fundamental earlier terminating the script then y'all can call input() method with a message at the stop of the script. The following script shows how y'all can pause the termination of the script and wait for the user's input. The input() method will take cord data and store in the variable, name. If the variable is not empty then a welcome message will print otherwise an fault bulletin will print. Next, an instruction bulletin will print to inform the user to press any key. A termination bulletin will print when the user will press any key.

#!/usr/bin/env python3

# Take an user input
proper noun = input ( "What is your name? " )
# Bank check the input value

if (name != "" ):
# Print welcome message if the value is not empty
print ( "How-do-you-do %south, welcome to our site" %proper name )
else:
# Print empty message
print ( "The name can't be empty." )

# Wait for the user input to terminate the program
input ( "Printing whatsoever central to cease the program" )
# Print bye message
print ( "See you later." )

Output:

After executing the script, it waits for a cord 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 afterward pressing any key.

Pause the input for a certain period of time

sleep() method tin be used to pause the user input for a certain period of time. In the following script, a elementary add-on task is given for the user. slumber() method is used hither to expect for the user for v seconds before typing the answer. Side by side, if the status is used to check the respond is right or incorrect.

#!/usr/bin/env python3

# Import fourth dimension module
import time

# Print the message
impress ( "Solve the problem to bear witness that you are a homo existence." )

# Print the question
impress ( "What the sum of x and 40? " )

# Print wait message
print ( "Waiting for 5 seconds for calculating ..." )
# Await for two seconds
time.slumber ( v )

# Accept input from the user
answer = input ( "Your answer: " )

# Check the answer
if ( int (respond) == 50 ):
impress ( "Your respond is right. Well done." )
else:

print ( "Y'all take failed to prove." )

Output:

A question will print later running the script and inform the user to wait for five seconds to find out the answer. Here, the script is executed two times with a correct reply and an incorrect reply.

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 procedure of providing information to the user by using multiple messages is shown in the post-obit script. The steps to run any python script are shown here by using multiple input() method. The user has to press any primal to show the adjacent steps. The first input() method is used to get-go showing the message and the last input() method is used to show the termination message.

#!/usr/bin/env python3

# Print the starting message
impress ( "The steps to run a python script:" )
# Look for any keypress
input ( "Press whatever central to continue" )
# Wait for any keypress
input ( "Write the script in whatever editor." )
# Await for any keypress
input ( "Open the final 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 whatever key to terminate." )
# Print the termination message
print ( "\nGood Goodbye." )

Output:

The following output will appear subsequently running the script. The user has to press whatever key 5 times to complete the execution of the script.

Suspension the script for the particular input value

If y'all want to run some scripts continuously until the user printing any specific fundamental then y'all have to define that script within any infinite loop. This job 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 cease of the loop, it will wait for the user to press 'y' to go on the loop and echo the script again.

#!/usr/bin/env python3

# Define a space loop
while ( Truthful ):

# Take two integer numbers
10 = int ( input ( "Enter a number: " ) )
y = int ( input ( "Enter a number: " ) )

# Add two numbers
result = x + y
# Print the summation result
impress ( "The sum of %d and %d is : %d" %(ten, y, result) )

# Wait for the user input to continue or terminate the loop
ans = input ( "Exercise y'all want exercise 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 hither. The first time, later on calculating the summation, 'y' is pressed and the script of the loop is repeated again. When the user pressed 'n' and then the loop is terminated.

Conclusion:

Pause for the user input is a very mutual requirement of whatever programming linguistic communication. Different purposes of intermission for the input are shown in this article by using very simple python examples. I promise, this article will help the reader to know the uses of pause for the input and use it in the script when requires.

About the author

I am a trainer of web programming courses. I like to write article or tutorial on various It topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Aid.

leflerquission.blogspot.com

Source: https://linuxhint.com/python_pause_user_input/

Post a Comment for "Python Closes at End of Windows q python Input to Run Again"