You can create a while with user input-based value evaluation with conditions. Just need to take input from the user and evaluate those values in the while loop expression condition.
Example while loop user input in Python
A simple example code takes input from the user and adds values into a list until a quit is entered by the user.
names =  new_name = '' # Start a loop that will run until the user enters 'quit'. while new_name != 'quit': new_name = input("Enter Name, or 'quit': ") if new_name != 'quit': names.append(new_name) print(names)
Check if the input name matched.
name = "not_aneta" while name != "aneta": name = input("What is my name? ") if name == "aneta": print("You guessed my name!")
What is my name? aneta
You guessed my name!
while True: user_input = input("Enter a number (or 'q' to quit): ") if user_input == 'q': break # Exit the loop if the user enters 'q' # Perform some operation with the user input number = int(user_input) square = number ** 2 print("The square of", number, "is", square)
In this example, the loop will continue indefinitely until the user enters ‘q’. Inside the loop, the user’s input is stored in the
user_input variable. If the user enters ‘q’, the loop is exited using the
break statement. Otherwise, the program performs some operation with the user input (in this case, squaring the number) and prints the result.
Get user input to stop a while loop
x = "" while x != "0": x = input("Enter 0 to exit: ") if x == "0": print("Stop the loop!")
Enter 0 to exit: 0
Stop the loop!
Do comment if you have any doubts or suggestions on this Python while loop topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.