This is a simple program I am working on to learn python because I am a beginner. How would I add an exception if the user types something other that y,Y,n,N. I have searched everywhere but can't seem to find what exception to use? Thanks for everyone's help.
EDIT:I adjusted my code. The only thing that is not working is if(welcomeString.strip().lower() != 'n' or 'y'): welcomeString = input('Not a valid choice\nWould you like to reverse a string?(y/n):'). It does not realize the user types in y or n. It works for other letters though.
EDIT2: Everything is working as expected until the user types in an invalid input a second time. The first time it will say "Not a valid choice", but the second time, the program will exit out.
import sys
welcomeString = input('Welcome to String Reverser\nWould you like to reverse a string?(y/n)')
if not welcomeString.strip().lower() in ['n','y']:
welcomeString = input('Not a valid choice\nWould you like to reverse a string?(y/n):')
if welcomeString.strip().lower() == 'n':
print("Thanks for using String Reverser")
while welcomeString.strip().lower() == 'y':
myString = input("What string would you like to reverse?:")
ReverseString = myString[::-1]
print (("Your Reversed String is %s") % ReverseString)
welcomeString = input("Would you like to reverse another string(y/n)")
welcomeString.lower().startswith('y')instead ofwelcomeString.startswith("y") or welcomeString.startswith("Y")welcomeString[0].lower() in {'y', 'n'}IndexError.