0

I am writing a heads or tails guessing program.

I am trying to figure out how to make the input that the user gives case insensitive and I've tried .upper() and .lower() and I'm just not sure how to do it.

import random
coin = ['Heads', 'Tails']
guess_count = 0
while guess_count <= 5:
    flip = random.choice(coin)
    guess = input("Guess Heads or Tails? ")
    guess_count += 1
    if guess_count == 5:
        print("Out of attempts")
        break
    elif guess == 'Quit':
        break
    elif guess == flip:
        print(f"It was {flip} good job!")
        print(f"You got it in {guess_count} attempt(s).")
        break
    else:
        if guess != flip:
            print("Try again!")
            print(f"Guess Count: {guess_count}")
print("Game Over!")
William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33

5 Answers5

1

Try using capitalize() It will make your string to have first letter capital only.

Try:

import random
coin = ['Heads', 'Tails'] 
guess_count = 0
while guess_count <= 5:
    flip = random.choice(coin)
    guess = input("Guess Heads or Tails? ").capitalize() #<--here
    guess_count += 1
    if guess_count == 5:
        print("Out of attempts")
        break
    elif guess == 'Quit':
        break
    elif guess == flip:
        print(f"It was {flip} good job!")
        print(f"You got it in {guess_count} attempt(s).")
        break
    else:
        if guess != flip:
            print("Try again!")
            print(f"Guess Count: {guess_count}")
print("Game Over!")
Pygirl
  • 12,969
  • 5
  • 30
  • 43
0

In the line

coin = ['Heads', 'Tails']

you made the decision that your choices are Heads and Tails, with a capital letter. The user will have to enter it exactly that way.

Using .lower() and .upper() will work - but (!) you have to do so on both sides. That is:

elif guess.lower() == flip.lower():

Usually, one would avoid that on one side by defining

coin = ['heads', 'tails']    # Note the lower casing
[...]
elif guess.lower() == flip:  # no lower casing needed on flip

But, if you want the output of print() to have exactly that casing, just use lower() or upper() on both sides of the comparison.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
0

you could do this:

coin = ['heads', 'tails']
guess = input("Guess Heads or Tails? ").lower()

You should also have something to check if the input are correct.

example for a simular flip a coin program:

import random

coin = ['heads', 'tails']
number_of_guesses = 5
coin_flip = random.choice(coin)
guesses=0
while (guesses<5):
    guess = input('Guess Heads or Tails? ').lower()
    if(guess==coin_flip):
        print(f'Correct, it was {coin_flip}')
        break
    elif(guess not in coin):
        guesses += 1
        print(f'wrong input, number of remainding guesses {number_of_guesses-guesses}')  
    
    else:
        guesses += 1
        print(guesses)
        if(guesses!=number_of_guesses):
            print(f'guess again, number of remainding guesses {number_of_guesses-guesses}')

if(guesses == number_of_guesses):
    print('you have failed')

This could also be solved recursivly/with a for loop, if you are interested in learning

vegiv
  • 124
  • 1
  • 9
0

You could also use casefold(), it is there to for caseless comparisons. And also the last "if" statement is not necessary

else:
     print("Try again!")
     print(f"Guess Count: {guess_count}") 
0
# Input is converted to lowercase
guess = input("Guess Heads or Tails? ").lower()

or

# Input is converted to uppercase
guess = input("Guess Heads or Tails? ").upper()

Use .lower() though, its easier imo

darylvickerman
  • 572
  • 5
  • 12