'Random word guessing game

I want to create a word guessing game where the program randomly selects a word from my word list and the user has to guess the word.

  • User can only guess one letter at a time.
  • User is only allowed to have 6 failed guesses. (Loses when 6 failed attempts are used).
  • User wins if he guess the complete word before 6 failed attempts is used.

So I'm facing quite a number of problems with my program:

  1. How do I make the guessed letter stay on the blanks when it goes to the next round of guess?
  2. If the word has two of the same letters, how do I display it on my blanks too?
  3. How do I show all the user's missed letters for each round?

Here's what I did so far:

import random

wordlist = ['giraffe','dolphin',\
            'pineapple','durian',\
            'blue','purple', \
            'heart','rectangle']

#Obtain random word
randWord = random.choice(wordlist)

#Determine length of random word and display number of blanks
blanks = '_ ' * len(randWord)
print ()
print ("Word: ",blanks)


#Set number of failed attempts
count = 6

#Obtain guess
while True:
    print ()
    guess = input ("Please make a guess: ")   
    if len(guess) != 1:
        print ("Please guess one letter at a time!")
    elif guess not in 'abcdefghijklmnopqrstuvwxyz':
       print ("Please only guess letters!")

#Check if guess is found in random word
    for letters in randWord:
        if guess == letters:
            letterIndex = randWord.index(guess)
            newBlanks = blanks[:letterIndex*2] + guess + blanks[letterIndex*2+1:]
            print ("Guess is correct!")
        else:
            count -=1
            print ("Guess is wrong! ", count, " more failed attempts allowed.")
    print() 
    print("Word: ",newBlanks) 

The results I hope to obtain (for randWord 'purple'):

Word: _ _ _ _ _ _ 
Missed: 
Please make a guess: l
Guess is correct!


Word: _ _ _ _ l _ 
Missed:
Please make a guess: z
Guess is wrong! 5 more failed attempts allowed.


Word: _ _ _ _ l _ 
Missed: z
Please make a guess: o
Guess is wrong! 4 more failed attempts allowed.


Word: _ _ _ _ l _ 
Missed: z, o
Please make a guess: p
Guess is correct!


Word: p _ _ p l _ 
Missed: z, o
Please make a guess: e
Guess is correct!


Word: p _ _ p l e 
Missed: z, o
Please make a guess: r
Guess is correct!


Word: p _ r p l e 
Missed: z, o
Please make a guess: u
Guess is correct!


Word: p u r p l e 
YOU WON!


Solution 1:[1]

How do I make the guessed letter stay on the blanks when it goes to the next round of guess?

Just store string containing guessed letter and blanks for next round. You are recalculating it every time from wordlist (it can be also recalculated every time, but then you need to modify your search function for letters, see answer 2)

If the word has two of the same letters, how do I display it on my blanks too?

Modify your search loop, it should continue searching after first matching letter is found.

letterIndex = randWord.index(guess) will return only first occurrence of guess in string.

How do I show all the user's missed letters for each round?

Store them in separate string or list. So you can just print it every time.

Solution 2:[2]

Instead of reusing the newBlanks string from the previous round, I suggest building it anew with join and a simple list comprehension, using a string guessed holding all the guesses, like here. Also note that your check for correct/incorrect letters does not work this way, but will decrease count for each letter of the word that is not the guessed letter. Use if guess in randWord: instead. Also, you can use count for the condition of the while loop, and continue with the next iteration of the loop if guess is not a single letter.

Putting it all together, your code could then look like this:

guessed = ""
while count >= 0:
    guess = input ("Please make a guess: ")   
    # ... check guess, continue if not a letter
    guessed += guess

    if guess in randWord:
        # ... print 'correct', else 'not correct', decrease count

    newBlanks = " ".join(c if c in guessed else "_" for c in randWord)
    print("Word: ",newBlanks) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nickzam
Solution 2 Community