Python Forum
Need some help creating a word game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help creating a word game
#1
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 !
Reply
#2
[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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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?
Reply
#4
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
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scramble word game Zatoichi 9 8,418 Sep-20-2021, 03:47 PM
Last Post: jefsummers
  problem on creating a small game nayo43 5 2,754 Dec-13-2020, 01:03 PM
Last Post: jefsummers
  Code: Creating a basic python game? searching1 5 3,435 Nov-12-2018, 05:18 AM
Last Post: searching1
  Word based Game of 21 DatNerdKid 2 76,670 Aug-24-2018, 03:25 PM
Last Post: DuaneJack
  scrabble word game atux_null 4 8,744 Nov-10-2017, 10:00 AM
Last Post: atux_null

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020