secret_word = "Decor"
secret_word = secret_word.lower()
guess = input("Enter your guess: ")
guess = guess.lower()
tries = 0
if secret_word == guess:
tries = 1
print("Correct! You got it in", tries, "tries!")
else:
tries = 2
print("That is incorrect. You have", tries, "tries left.")
for char in secret_word:
if char in guess:
print("Hint: The following letters are in the secret word - ", char)
How do I check a letter from the user input is in the secret word
CodePudding user response:
Check if guess
exists as a substring
if guess in secret_word:
print(f"found '{guess}' in {secret_word}")
Check if any characters overlap (regardless of order)
if any(c in secret_word for c in guess):
print(f"found letter from '{guess}' in {secret_word}")
Check if all characters exist (regardless of order)
if all(c in secret_word for c in guess):
print(f"found all letters from '{guess}' within {secret_word}")
Limit your inputs to letters
guess = input("Enter your guess: ")
guess = guess.lower()
if len(guess) > 1:
raise ValueError('input a letter')
CodePudding user response:
Your code has no loop, so you only get one try, no matter what. Due to that I had to made the assumption that you are basing this all on tries. With that assumption I fleshed your example out to a MRE. The answer you asked for is commented in the code,.
MAXTRIES = 3
secret_word = "decor"
tries = 0
output = ("Correct! you got it in %i tries!",
"That is incorrect. You have %i tries left.")
while tries<MAXTRIES:
tries = 1
guess = input("Enter your guess: ").lower()
wrong = secret_word != guess
print(output[wrong] % abs(MAXTRIES*wrong-tries))
if wrong:
#this line is the actual answer to your question
#you should get all of the characters before you print them
chars = [char for char in secret_word if char in guess]
print("Hint: The following letters are in the secret word - ", chars)