In this tutorial we will learn about python while loop with user input. While loop is used to iterate over program until the condition is satisfy and input() is used to take value from user.
Sometime in program we required input from user when your while loop is running that time we can use input() function.
Now let see the python code which used input inside a While Loop.
Program to take Age for Voting by using python while loop user Input
while True: try: age = int(input("Please enter your age: ")) except ValueError: print("Sorry, I didn't understand that.") continue else: break if age >= 18: print("You are able to vote!") else: print("You are not able to vote")
Explanation: In the above program we are taking age from user as an input and then check is age is > 18 if yes then print “You are able to vote” else print ”you are not able to vote”.
name = "not_programiz" while name!= "programiz": name = input("What is my name? ") if name == "programiz": print("You guessed my name!")
Explanation: In the above guessing code first we declared variable called “name” and assigned Default value to it called “not programiz“ after that we run while loop and inside while loop checking is name=”programiz” or not, if yes then it will print “you guessed my name” otherwise keep trying because your while loop is running.