1

Here is the problematic piece of my function:

def hangman1(word):
global guessesMade
global guessesLeft
currentGuess = '_ ' * len(word)
let = print(input('Please guess a letter: '))
for i in range(len(word)):
    if word[i] == let:
        print('{} is contained in the word.'.format(let))
        if i == 0:
            currentGuess = word[0] + currentGuess[1:]
        else:
            currentGuess = currentGuess[:i] + word[i] + currentGuess[i + 1:]
print(currentGuess)

The user enters a letter at the prompt and it checks if the letter is in the randomWord that was generated outside of the function from a list of words. I can get it to print the blanks correctly, but if the user enters a letter that is in the word it prints out a line of the correct letter instead of the blanks with the correct letter mixed in between.

Any help is appreciated.

Droxbot
  • 53
  • 1
  • 10
  • You can't use the `replace` function directly; you will need to slice as well. See here: http://stackoverflow.com/questions/12723751/replacing-letter-in-string-by-index – 101 Oct 29 '14 at 02:57
  • not sure why this question was downvoted. the OP attempted to solve the problem him/herself, gave a clear description of where he is stuck & what he tried. As a first post, this is pretty good. i'm voting it up. – Haleemur Ali Oct 29 '14 at 20:21

2 Answers2

2

The main problem you're having right now is two-fold - one, that the replace() method replaces all instances of any given input within a string, not the first one, and two, that you don't currently have any way of telling which letters you've already uncovered. Calling replace("_", let) will always replace every single instance of "_", and given that you're applying that to a string that is only composed of underscores, it'll always overwrite the entire string. It seems like you're also regenerating hidden_let every time hangman() is called with a guess letter, meaning that best-case with your design now you're only going to ever show every letter the user just guessed and a bunch of underscores otherwise.

What you'd want to do is have two values, correct_word and current_guess. correct_word will be the word the player has to guess, and current_guess will be their progress in guessing the word, starting with a string of only underscores of the same length as correct_word.

Here's a short example. I've taken the liberty of removing your global references - globals are generally frowned upon - and encapsulated the behavior in a small class. You'd want to replace the value in hangmanner.play_hangman() with whatever your random word is.

class Hangmanner:
    correct_word = ''    
    current_guess = ''

    def play_hangman(self, word):
        self.correct_word = word

        self.current_guess = '_' * len(self.correct_word)

        while self.current_guess != self.correct_word:
            self.guess_letter(input("Please guess a letter: "))

    def guess_letter(self, guessed_letter):
        for i in range(len(self.correct_word)):
            if self.correct_word[i] == guessed_letter:
                if i == 0:
                    self.current_guess = self.correct_word[i] + self.current_guess[1:]
                else:
                    self.current_guess = self.current_guess[:i] + self.correct_word[i] + self.current_guess[i + 1:]
        print(self.current_guess)

if __name__ == "__main__":
    hangmanner = Hangmanner()
    hangmanner.play_hangman("test")

This uses the slicing function in python, where you can use the brackets and the [first:last] syntax to access an arbitrary range of any given collection. If either first or last is missing, the slice continues to the beginning or end of the collection, respectively. Above, current_guess[1:] returns current_guess from the second index to the last. current_guess[:i] returns current_guess from the first index up to the index preceding i, given that last is the exclusive end bound.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • I can add the rest of the function if it will be helpful. – Droxbot Oct 29 '14 at 03:01
  • Where/what? Your post looks identical, and nothing showed up in your comment. – furkle Oct 29 '14 at 03:54
  • I updated the function in the original question. It is somewhat working, but it will still not print out in the correct format. Maybe I messed up somewhere along the way. – Droxbot Oct 29 '14 at 03:54
  • I've added the context for what I'd posted up there. Does that clarify it? – furkle Oct 29 '14 at 04:06
  • If it was helpful, please don't forget to upvote (with the up arrow) and select this answer (by clicking the check mark) – furkle Oct 29 '14 at 04:17
2

hiddenLet.replace('_',let) replaces all occurrences of _ with whatever let represents.

newWordList = [x if x==let else '_' for x in randWord] 
newWord = ''.join(newWordList)
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85