How could I make checking user input case-insensitive?
Question:
I am adding a few ways of checking user input to my code. Instead of writing if Answer == ("Yes") or Answer == ("yes")
, is there any way I could make the variable input case-insensitive, so that I don’t have to type the word yes twice? Additionally, would there be any way I could make the every variable input option in the line case-insensitive? if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
.
def Repeat():
Answer = input ("nDo you want to play again? (Y/N): ")
if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
print ("")
if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
exit()
if Answer == "Options" or Answer == "options" or Answer == "o" or Answer == "O":
print ("")
else:
print ("Invalid Input")
Repeat()
Repeat()
Answers:
You can change the case of the user input and just check against that:
answer = input("nDo you want to play again? (Y/N): ").lower()
if answer in ("yes", "y"):
...
I am adding a few ways of checking user input to my code. Instead of writing if Answer == ("Yes") or Answer == ("yes")
, is there any way I could make the variable input case-insensitive, so that I don’t have to type the word yes twice? Additionally, would there be any way I could make the every variable input option in the line case-insensitive? if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
.
def Repeat():
Answer = input ("nDo you want to play again? (Y/N): ")
if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
print ("")
if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
exit()
if Answer == "Options" or Answer == "options" or Answer == "o" or Answer == "O":
print ("")
else:
print ("Invalid Input")
Repeat()
Repeat()
You can change the case of the user input and just check against that:
answer = input("nDo you want to play again? (Y/N): ").lower()
if answer in ("yes", "y"):
...