Guess the Letter Program Not Working?

时间:2018-04-20 21:19:30

标签: python

I'm creating a very basic "guess the letter" game. My code is below. When I run the program and enter one of the correct listed letters, it still runs the code as if it was an incorrect letter. What do I need to fix?

letters = ("M","K","O","W","X","S","A")

guess = input("Guess a letter: ")

while guess.lower() != letters:
    print("Incorrect!")
    input("Guess a letter: ")
    if guess.lower() == letters:
        print(guess,"is correct!")

input("Press Enter to Continue")

When the code is working properly, it should display something like this:

Guess a letter: p
Incorrect!
Guess a letter: q
Incorrect!
Guess a letter: m
M is correct!

1 个答案:

答案 0 :(得分:3)

You need to compare the user's guess with the uppercase letters in letters using guess.upper() not guess.lower(). You should also compare the guessed letter using in rather than checking for equality with a tuple. Finally, you will need to store the user's updated guess again within the while statement:

letters = ("M","K","O","W","X","S","A")

guess = input("Guess a letter: ")

while guess.upper() not in letters:
    print("Incorrect!")
    guess = input("Guess a letter: ")

print(guess,"is correct!")
input("Press Enter to Continue")