Search code examples
pythoncase-sensitive

How to can I make an input completely case insensitve (covering all possible variations)


new starter here. I'm trying to run a simple guessing game where the input from the user wont be case sensitive, so i want the user to get the "you win" result regardless of inputting "Lion", "lion", "LiOn" etc.. i tried everything i could think about and failed. thank you.

secret_animal = "Lion"
guess = ""

    while guess != secret_animal:
        guess = input("Guess the secret animal: ")
        if guess != secret_animal:
            print("Bad luck try again")
            print("Hint: Some call it the King")

print("You guessed it!! YOU WIN")`

Solution

  • A simple way of doing this is to set both guess and the secret_animal to lower case and then compare them. You can do this by using the .lower() function.

    secret_animal = "Lion"
    guess = ""
    
    while guess.lower() != secret_animal.lower():
        guess = input("Guess the secret animal: ")
        if guess.lower() != secret_animal.lower():
            print("Bad luck, try again")
            print("Hint: Some call it the King")
    
    print("You guessed it!! YOU WIN")