Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How could I make checking user input case-insensitive?

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"):
    Game()
  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":
    More()
  else:
    print ("Invalid Input")
    Repeat()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"):
     ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading