-1

I'm new to programming and so I'm trying out some stuff to see if I actually understood. I was able to make this "program" non-case sensitive, but really poorly (since I had to make 3 variables for the same answer, just to make it non-case sensitive). How can I make this code better?

fav_color = "Red"
fav_color2 = "RED"
fav_color3 = "red"
guess_count = 0
guess_limit = 3

while guess_count < guess_limit:
    x = input("What's Carlos' favorite color? ")
    guess_count += 1

    if x == fav_color:
        print("You win!")
        break
    if x == fav_color2:
        print("You win!")
        break
    if x == fav_color3:
        print("You win!")
        break
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
Onclax
  • 3
  • 2

2 Answers2

-1

Convert the string returned from input to lowercase with the .lower() string method.

That is to say,

fav_color = 'red'
guess_count = 0
guess_limit = 3

while guess_count < guess_limit:
    guess = input('What's Carlos' favorite color? ').lower()
    guess_count += 1
    if guess == fav_color:
        print("You win!")
        break

Just to give you a feel for how the .lower() method works, I'll provide some examples:

>>> 'Hello'.lower()
'hello'
>>> 'hello'.lower()
'hello'
>>> 'HELLO'.lower()
'hello'
>>> 'HeLLo HOW aRe YOu ToDAY?'.lower()
'hello how are you today?'
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
-1

Use str.lower() (or upper())

fav_color = "Red"

guess_count = 0
guess_limit = 3

while guess_count < guess_limit:
    x = input("What's Carlos' favorite color? ")
    guess_count += 1

    if x.lower() == fav_color.lower():
        print("You win!")
        break
rdas
  • 20,604
  • 6
  • 33
  • 46