0

I have a game where the user guesses letters. They are shown a blank version of the mystery work (_____ for example, the _'s are equal to number of characters in the word). The program knows the word, and needs to replace every index in the blanked out version of the word if the letter they guess is present in the mystery word.

For example, if the player guesses "p" and the word is "hippo" they will be shown __pp_. But, my code will only replace the first instance of "p", giving __p__ instead.

Would this be easier to tackle as a list problem?

mistakes = 0
complete = False
t = False
words = ['cow','horse','deer','elephant','lion','tiger','baboon','donkey','fox','giraffe']

print("\nWelcome to Hangman! Guess the mystery word with less than 6 mistakes!")


# Process to select word
word_num = valid_number()
word = words[word_num]
#print(word)
print("\nThe length of the word is: ", str(len(word)))
attempt = len(word)*"_"

# Guesses
while not (mistakes == 6):
    guess = valid_guess()
    for letter in word:
        if guess == letter:
            print("The letter is in the word.")
            position = word.index(guess)
            attempt = attempt [0:position] + guess + attempt [position + 1:]
            print("Letters matched so far: ", attempt)
            t = True
    while (t == False):
        print("The letter is not in the word.")
        print("Letters matched so far: ", attempt)
        mistakes = mistakes + 1
        hangMan = ["------------", "|          |", "|         O", "|       /   |", "|          |", "|       /   |\n|\n|"]
        hang_man()
        t = True
    t = False
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user3290553
  • 87
  • 1
  • 2
  • 9

2 Answers2

2
answer = 'hippo'
fake = '_'*len(answer)   #This appears as _____, which is the place to guess
fake = list(fake)        #This will convert fake to a list, so that we can access and change it.
guess = raw_input('What is your guess? ')   #Takes input
for k in range(0, len(answer)):  #For statement to loop over the answer (not really over the answer, but the numerical index of the answer)
    if guess == answer[k]  #If the guess is in the answer, 
        fake[k] = guess    #change the fake to represent that, EACH TIME IT OCCURS
print ''.join(fake)  #converts from list to string

This runs as:

>>> What is your guess?
p
>>> __pp_

To loop over everything, I did not use index, because index only returns the first instance:

>>> var = 'puppy'
>>> var.index('p')
0

So to do that, I analyzed it not by the letter, but by its placement, using a for that does not put k as each letter, but rather as a number so that we can effectively loop over the entire string without it returning only one variable.

One could also use re, but for a beginning programmer, it is better to understand how something works rather than calling a bunch of functions from a module (except in the case of random numbers, nobody wants to make their own pseudo-random equation :D)

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

Based on Find all occurrences of a substring in Python:

import re

guess = valid_guess()
matches = [m.start() for m in re.finditer(guess, word)]
if matches:
    for match in matches:
        attempt = attempt[0:match] + guess + attempt[match+1:]
        print("Letters matched so far: ", attempt)
else:
    .
    .
    .
Community
  • 1
  • 1
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52