Hiding letters of a word

Question:

the code is choosing a word from the list based on the conditions. but it’s a game and i want to word would be displayed as something like this when for example if the random choose is wolf i want it to be like this ^^^^. what should i do?the purpose is a game that user can guess the letter. at first word is like this "^^^^" after that when user guess if the letter is right forexample "r" is right it reveals in the word at rights position like this "^^r^".

    from random import choice
# Creating two lists that contain 4 letter and 5 letter words
word4 = ["bear" ,"bird" ,"clam" ,"crow" ,"deer" ,"duck" ,"frog" ,"goat" ,"kudu" ,"lamb" ,"lion" ,"lynx" ,"mole" ,"mule" ,"newt" ,"orca" ,"seal" ,"swan" ,"toad" ,"wolf"]
word5 = ["ratel", "zebra", "mouse", "sheep", "whale", "camel", "cobra", "eagle", "goose", "llama", "moose", "raven", "rhino", "shark", "sloth", "snake", "stork", "tiger", "trout"]


print("Welcome to Guess-The-Letter game")
print("")


while True:
    # Asking for user to choose number of guess
    num_guess = input("how many times do you want to guess [1-10]: ")
    try:
        num_guess = int(num_guess)
    except ValueError:
        print("Not a number!")
        continue  # Checking for non number input
    if num_guess < 1 or num_guess > 10:
        print("Out of Range")  # Checking for out of range input
    else:
        result1 = int(num_guess) # Storing the value
        break  # Anything else would be in range that we want and will store the value and break the loop
while True:
    # Asking for user to choose length of word
    length = input("please enter the length of the word [4-5]: ")
    try:
        length = int(length)
    except ValueError:
        print("Not a number!")
        continue  # Checking for non number input
    if length < 4 or length > 5:
        print("Out of Range")  # Checking for out of range input
    else:
        result2 = int(length) # Storing the value
        break  # Anything else would be in range that we want and will store the value and break the loop


# Choosing the words
if result2 == 4:
    word = choice(word4)
else:
    word = choice(word5)
while result1 > 0:
    print("word is:", word)
    print("Guess remaining:", num_guess)
    print("Previous guess:", guess)
    guess = input("Please guess a letter: ")
Asked By: Yashar Biabani

||

Answers:

If you want that the user have to write the whole word you can do the following

def check_guess(oracle_word, guess) -> bool:
    for i, char in enumerate(guess):
        if char == oracle_word[i]:
            word[i] = char
    return False if "*" in word else True


guess = ""
word = []
# Choosing the words
if result2 == 4:
    oracle_word = choice(word4)
else:
    oracle_word = choice(word5)
for i in range(0, len(oracle_word)):
    word.append("*")
while result1 > 0:
    print("word is:", "".join(word))
    print("Guess remaining:", num_guess)
    print("Previous guess:", guess)
    guess = input("Please guess a letter: ")
    if check_guess(oracle_word, guess):
        print(f"The word is {oracle_word} good job!")
        exit(1)

The output is going to be

Welcome to Guess-The-Letter game

how many times do you want to guess [1-10]: 1
please enter the length of the word [4-5]: 4
word is: ****
Guess remaining: 1
Previous guess: 
Please guess a letter: beat
bea*
word is: bea*
Guess remaining: 1
Previous guess: beat
Please guess a letter: bear
The word is bear good job!
enter code here

If you want that the user have to insert just some letters you can change the check_guess function to this

def check_guess(oracle_word, guess) -> bool:
for i, char in enumerate(guess):
    if char in oracle_word:
        indexes = [i for i, oracle_char in enumerate(oracle_word) if oracle_char == char]
        for index in indexes:
            word[index] = char
return False if "*" in word else True
Answered By: mamo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.