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 OverflowVideos
while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.
Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.
my question: while WHAT is True?
While True is True.
The while loop will run as long as the conditional expression evaluates to True.
Since True always evaluates to True, the loop will run indefinitely, until something within the loop returns or breaks.
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
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.
I remember seeing a video where the programmer said that using while True loops aren't good.
Can someone give an example of where this might be the case?
What would be better to use? especially, if I am trying to check for user input.
E.g. They have entered a username that is not in the list of usernames.