Python Forum

Full Version: Need some help creating a word game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I’ve recently started to learn about Python (for about 2 months), and I need some help to create a word based game, similar to a game called “Wordle”, I was able to do most of the coding, but I'm having trouble creating the hints part, for that I'm supposed to do the following:


Letters that are not present at all in the secret word show a "underscore _".

Letters that are present in the secret word, but in a different spot show in 'lowercase".

Letters that are present in the secret word at that exact spot, show in "uppercase".

Until now my code looks like this:

import random

random_words = ("house" "sound", "television", "family", "microwave", "water")
secret_word = random.choice(random_words)
guess = ""
guess_count = 0
hint = "_ " * len(secret_word)

print("Welcome to the guessing game !")
print()

while guess != secret_word:
    print(f"Your hint is {hint}")
    guess = input("What is your guess? ").lower()
    if len(guess) != len(secret_word):
        print("Please enter the right lenght!")
    guess_count = guess_count + 1

else:
    print("Congratulations You guessed it!")
    print()
print(f"It took you {guess_count} guesses to get it right!")
What I need to do looks something like this:

[Image: lpJBRzV.png]

If someone can help me with this, I will be grateful !
[content removed]

Sorry; I think that I'm leading you up a blind alley with what I had in mind, so I redact what I said.
I think you are wrong about the mosiah moroni example. The correct hint should be M O_ _ _ _ . M O _ o _ i gives the impression that the actual word contains two "o"'s.

Marking correct letters in the correct position is easy. If word[i] == guess[i] it should be capital letter. Tougher is letters that are in the word, but not in the correct position. As the mosiah/moroni example shows, you need to do something to prevent using the same letter in the word for multiple matches in the guess. If you were doing this with pencil and paper you would do something like cross out the letters as they are used. Can you think of a Python equivalent?
Ah, I think I see what you're driving at, but it's a little hard to explain, without giving too much away.

I now have a script that produces a hint_list, which initializes with a _ for each letter position. I then loop through the guess, with for index, letter in enumerate(guess): and use the methods .pop() or .insert() for the index positions, with if and elif branches; all of which is controlled by a while: clause to track the index usage.

I don't think that is giving too much away.

As a proof of concept:
Output:
word : c o u n t e r s guess: c o m p u t e r hint : C O _ _ u t e r
(Oct-31-2022, 10:15 PM)wthiierry Wrote: [ -> ]Letters that are not present at all in the secret word show a "underscore _".

Letters that are present in the secret word, but in a different spot show in 'lowercase".

Letters that are present in the secret word at that exact spot, show in "uppercase".

It's always good to have a plan. So:

Letters that are present in the secret word at that exact spot, show in "uppercase".

There are many ways to achieve that but one of them is iterating over secret word and guess in parallel using built-in zip and check if characters are equal:

for guess, secret in zip(guess_word, secret_word):
    if guess == secret:
        # do something
Letters that are present in the secret word, but in a different spot show in 'lowercase".

If we already iterate over word we should also check whether character from guess_word is in secret_word. We can check it directly or use Python special data type for membership testing - set. In order to avoid overwriting result of if branch we should put this secondary check into elif branch ensuring that it will run only if characters are not equal:

elif guess in secret_word:
    # do something

# or

chars = set(secret_word)  # should be outside of for-loop

elif guess in chars:
    # do something
Letters that are not present at all in the secret word show a "underscore _".

If we have handled two cases in if and elif branch and anything that passes them is character what is not in same place nor in secret_word at all. So we can direct everything else to else branch:

else:
    # do something
Hint: using print function argument end we are able to print different loop iterations in one row:

for char in 'mosiah':
    print(char)
# will print:
m
o
s
i
a
h

for char in 'mosiah':
    print(char, end='')

# will print
mosiah
This of course assumes that validation of letters to be in same case and words are equal length has been made.