How to keep repeating a program until a specific input is obtained? | applied electronics engineering

Latest

How to keep repeating a program until a specific input is obtained?

By Applied Electronics - Sunday, April 9, 2017 No Comments
In this python programming tutorial we show how to keep repeating a program until a specific input is obtained. An often program requirement is to allow user to enter some valid input. If the input is not valid keep the program contentiously ask for valid input.

We can keep repeating a block of code or segment of code using the while loop. In the previous tutorial Calculating Summation using While Looop in Python programming language we illustrated while loop for doing summation equation. In similar fashion we can ask the user to input until they enter valid input.

The following python code keeps repeating asking the user to enter valid name.

name = ''

while name != 'pass':
    name = input('Please enter your name.')
   
print('thank you!')
  

Running this program we get:

Please enter your name.john

Please enter your name.ron

Please enter your name.jimmy

Please enter your name.pass
thank you!

So as you can see the program keeps asking the user to input until the specified input pass is obtained from the user.

For book see Head First Programming: A learner's guide to programming using the Python language.

No Comment to " How to keep repeating a program until a specific input is obtained? "