You want to repeat try-except... And the thing you want to try is input(), so you need to put basically all the code within the loop.

And remove the lower(), or convert to "a" and "n"

while True:
    try:
        name = input ("What is your name ? ").strip()
        if name.startswith("A"):
            print("message for A")
            break       
        elif name.startswith("N"):
            print("message for N")
            break
        else:
            print("Sorry, my only purpose is to talk to N and A")
    except ValueError:
        print ("Sorry, my only purpose is to talk to N and A")
Answer from OneCricketeer on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - To see this construct in practice, ... = "secret123" attempts = 0 while True: password = input("Password: ").strip() attempts += 1 if password == correct_password: print("Login successful!...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ while-loops-in-python-while-true-loop-statement-example
While Loops in Python โ€“ While True Loop Statement Example
July 19, 2022 - Something to note here is that the user input by default is case-sensitive, which means that if the user enters 'python' instead of 'Python' they still won't be able to continue. To fix this, you can use a string method such as .capitalize() to capitalize the first letter of the word the user enters. user_input = input("Please enter the secret keyword: ").capitalize() Next, it is time to construct the while loop.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
While true loop, i want to give the user another reply for his second try - Python Help - Discussions on Python.org
June 14, 2023 - In this case, if the user puts 'N' he will get back to the first line, right? if the user puts again 'N', I want to give him another reply for his second retry answer = input ("Stranger: Can you provide us your information ?(Y/N): ") .upper() while True : if answer == โ€œYโ€ : break if answer == โ€œNโ€: print ("please reconsider your your answer ") Continue else: print ("bruv, It is a Yes or No question!!
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - They are commonly used to create infinite scroll pages and interactive programs, where they are used to continuously check for new content or accept and process user input. You can create more dynamic and interactive programs in Python by ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
May 27, 2025 - Using while True creates an infinite loop that runs endlessly until stopped by a break statement or an external interruption. Its main uses include: Continuously prompt user input until valid data is entered
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-while-loop-tutorial
Python While Loop Tutorial โ€“ While True Syntax Examples and Infinite Loops
November 13, 2020 - We will the input() function to ask the user to enter an integer and that integer will only be appended to list if it's even. ... # Define the list nums = [] # The loop will run while the length of the # list nums is less than 4 while len(nums) ...
Find elsewhere
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-32066.html
While True Problem
I am new to python and I am trying some practice problems and I can get this one to work: -Prompt user for an integer between 1 and 10 and re-prompt if the input is not in this range -Print that many '&', one per line I know I need to use while Tru...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ while-loop
Python while Loop (With Examples)
If the condition is True, body of while loop is executed. The condition is evaluated again. This process continues until the condition is False. Once the condition evaluates to False, the loop terminates. Tip: We should update the variables used in condition inside the loop so that it eventually ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ using a while loop to ask the user (y/n) ?
r/learnpython on Reddit: Using a while loop to ask the user (Y/N) ?
October 31, 2023 -

So I have a program that calculates compound interest, and now I need to add a question at the end of my program that asks the user if they would like to run the program again, if they answer with Y or y the program will restart to the beginning, if they answer โ€œNโ€ or โ€œnโ€ the program is over. And if they answer with anything other than y or n it should repeat the question until the user finally enters y or n

What I have so far:

I have my entire program in a while loop and was able to get it to reset while Var == Y or y. But I canโ€™t get it to repeat the question if the user answers with letโ€™s say โ€œqโ€ or โ€œ618โ€ or โ€œLโ€. Does anyone know how to do something like this, please let me knowโ€ฆ thank you

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can someone explain what this `while true` function is actually checking?
r/learnpython on Reddit: Can someone explain what this `while True` function is actually checking?
December 13, 2023 -

https://pastebin.com/NWkKQc1P

It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?

PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.

Top answer
1 of 8
23
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ")
2 of 8
9
Correct; this is just a way to start an infinite loop that you intend to break with some condition/break statement later on. Mostly used for starting/running an application that you don't want to close down without user intent (clicking an exit/quit button in a UI or entering "quit", "exit", etc from the command line. It *isn't* a great construct to use when you want something that has solidly defined criteria to repeat until those criteria are met. As mopslik pointed out, you would be better served in those examples to actually use the defined criteria in those as your loop conditions since you're not at risk of just sticking the user in an infinite loop if you forget to write your break statement somewhere. You, also, don't have to write so many lines to accomplish the same goal. ``` password = "" while not password.isalnum(): password = input('Select a new password (alphanumeric only') print("Password is good") ``` You do not have to print a line and then call input(), either. Just put the message you want displayed into the parens for the function call: input("Enter your age: ")
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python while loop
Python while Loop - AskPython
February 16, 2023 - We can implement this feature using โ€œwhile Trueโ€ block and break statement. Here is an example of a utility script that takes the user input (integer) and prints its square value.
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
If the user enters a valid number, the program should print a message indicating that the number is valid and exit the loop. Use a continue statement to skip over any input that is not a valid number between 1 and 10.
๐ŸŒ
Learner
ahmedgouda.hashnode.dev โ€บ user-input-and-while-loops-in-python
User Input and While Loops in Python
May 8, 2022 - We can stop the while loop in this program by calling break as soon as the user enters the 'quit' value: prompt = "\nPlease enter the name of a city you have visited:" prompt += "\n(Enter 'quit' when you are finished.) " while True: city = input(prompt) if city == 'quit': break else: print(f"I'd love to go to {city.title()}!")
๐ŸŒ
Gishub
geohey.gishub.org โ€บ python โ€บ 07_user_input_while_loops
07 user input while loops - geohey
We can stop the while loop in this program by calling break as soon as the user enters the 'quit' value: ... prompt = "\nPlease enter the name of a city you have visited:" prompt += "\n(Enter 'quit' when you are finished.) " # while True: # city = input(prompt) # if city == 'quit': # break # else: # print(f"I'd love to go to {city.title()}!")
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example: n = raw_input("Please enter 'hello':") while n.strip() != 'hello': n = raw_input("Please enter 'hello':") However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following: while True: n = raw_input("Please enter 'hello':") if n.strip() == 'hello': break
๐ŸŒ
SQLPad
sqlpad.io โ€บ tutorial โ€บ python-while-loop
Python while loop - Python while loops, a key concept in programming. Learn how looping enables repeated code execution, a crucial tool for efficient and effective programming solutions - SQLPad.io
We can use a while loop that will continue to prompt the user for their age until they enter a valid number. while True: try: age = int(input("Please enter your age: ")) if age < 0: print("Age cannot be negative.