Python word guessing game not solving if full-word guess is provided

It’s working pretty well but when I try to guess the whole word, instead of just a letter, it doesn’t recognize anything past the first letter of the input.

I’m thinking the issue is somewhere around line 41, but I’m not entirely sure. Do I need to define an entirely new function for if a complete word is guessed?

import random



 def choose_eng_word():
    
       eng_wb = ["world", "woman", "place", "group", "problem", "house", "system", "thing", "point", "family", "fact", "money", "child", "water", "state", "month", "person", "company", "hand", "part", "instance", "government", "number", "question", "issue", "service", "friend", "example", "business", "job", "complication", "authority", "government", "development", "education", "community", "president", "hospital", "information", "research", "history", "communication", "experience", "decision", "support", "understanding", "relationship", "variation", "daughter"]
    
        eng_word = random.choice(eng_wb).upper()
    
        return eng_word
    
    def eng_word_in_prog(eng_word, guesses):
        eng_word_in_prog = "" 
        for letter in eng_word:
            if letter in guesses:
                eng_word_in_prog += letter
            else:
                eng_word_in_prog += "*"
        return eng_word_in_prog
    
    
    
    def eng_main(eng_word):
        lettersguessed = []
        chances = int(len(eng_word) + 4)
        print("you are looking for a word that is " + str(len(eng_word)) + " letters long.")
    
        while True:
            if chances != 0:
                print("\nYou have " + str(chances) + " chances left.")
                print("Word so far: " + eng_word_in_prog(eng_word, lettersguessed))
                print("letters guessed: " + str(lettersguessed))
                guess = input("Guess: ").upper()[0]
    
                if guess not in lettersguessed:
                    lettersguessed.append(guess)
        
                if eng_word_in_prog(eng_word, lettersguessed) == eng_word:
                    print("\nCongratulations! You guessed the correct word: " + eng_word)
                    break
                else:
                    chances -=1
                    if guess in eng_word:
                        print("Correct Letter!)")
                    else:
                        print("Incorrect! Guess again!")
            else:
                print("\nWhoops! You ran out of guesses. The correct word was " + eng_word)
                break
    

while True:
    eng_word = choose_eng_word()
    eng_main(eng_word) 
    if input("Would you like to continue?").lower().startswith("n"):
        break

I tried changing a bit of the language around Line 41, since it’s the area that references guessing a word, but anything I tried broke the code and I had to revert it to the original.

>Solution :

You can improve some things like:

  • Remove the [0] indexing from the input("Guess: ").upper() line to allow the input to be a complete word.
  • If the guess is equal to the entire word, it checks if it’s correct and breaks the loop if it is.
  • If the guess is a single letter, it continues the previous logic for checking if the letter is correct and updating the display.

Maybe you can improve the following part of your code.
Where you do this:

...
if guess == eng_word:
    print("\nCongratulations! You guessed the correct word: " + eng_word)
    break
elif len(guess) == 1 and guess not in lettersguessed:
    lettersguessed.append(guess)
    if guess in eng_word:
        print("Correct Letter!")
    else:
        print("Incorrect! Guess again!")
    if eng_word_in_prog(eng_word, lettersguessed) == eng_word:
        print("\nCongratulations! You guessed the correct word: " + eng_word)
        break
    chances -= 1
else:
    print("Invalid input. Please enter a single letter or the entire word.")
...

Try use this:

...
if guess not in lettersguessed:
    lettersguessed.append(guess)

if eng_word_in_prog(eng_word, lettersguessed) == eng_word:
    print("\nCongratulations! You guessed the correct word: " + eng_word)
    break
else:
    chances -=1
    if guess in eng_word:
        print("Correct Letter!)")
    else:
        print("Incorrect! Guess again!")
...

Leave a Reply