I need help fixing this hangman function

Brett

Okay, so I am stuck at this part in my code. When I want the letter that the user guesses to replace that letter in the string of underscores, it replaces every single letter with that letter. I don't know what to do. Here is the code.

 def hangman(secret):
    '''
    '''

    guessCount = 7
    w = '_'*len(secret)

    while guessCount > 0:
        guess = input('Guess: ')
        if guess in secret:
            indices = indexes(secret, guess)
            print(indices)
            for i in range(len(indices)):
                w = w.replace(w[indices[i]],secret[indices[i]])
                print(w)
        else:
            guessCount = guessCount - 1
            print('Incorrect.',guessCount,'incorrect guesses remaining.')

Any help in pointing out what I can do specifically in line 9 and 10 would be greatly appreciated.

Here is the first function that I defined earlier that I use in this function.

def indexes(word, letter):
    '''returns a list of indexes at which character letter appears in word'
    '''

    indices = []

    for i in range(len(word)):
        if letter in word[i]:
            indices.append(i)
    return indices
lightalchemist

Strings are immutable in Python. Hence, it is not a suitable data structure for representing your word. In my opinion, Kyle Friedline's approach is probably the right way.

def hangman(secret, guessCount=7):
    assert guessCount > 0  # Never really good to hard code magic numbers.

    w = ['_'] * len(secret)  # Make 'w' a list instead because list is mutable
    while guessCount > 0:
        guess = input("Guess: ")
        if guess in secret:
            indices = indexes(secret, guess)  # I'm guessing indexes is defined elsewhere?
            for i in indices:
                w[i] = guess  # Make it explicit. secret[i] == guess anyway.
            print("".join(w))  # Join w into a word
        else:
            guessCount -= 1  # More concise
            print("Incorrect. ", guessCount, " incorrect guesses remaining.")

A little suggestion for implementing indexes:

def indexes(word, letter):
    return [i for i, c in enumerate(word) if c == letter]

Or simply replace the call to indexes() with:

indices = [i for i, c in enumerate(secret) if c == guess]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I need help fixing this hangman function

From Dev

Need help fixing a game I made in Python

From Dev

Need help fixing the output of this Higher Order Function.

From Dev

I need help fixing my code - very fragile

From Dev

Need help hangman c#

From Dev

Need help in identifying and fixing SSLPeerUnverifiedException

From Dev

Need help fixing the syntax for this LEFT OUTER JOIN

From Dev

Need help fixing strange cin behavior

From Dev

Need Help Fixing A Bug In Snake Game

From Dev

Need help fixing setInterval issue in JavaScript

From Dev

Need CSS/HTML help fixing navigation in IE

From Dev

Need help fixing a segmentation fault (core dumped)

From Dev

Need help fixing my nginx server

From Dev

need help fixing 500 error python/flask app

From Dev

Need help fixing my error on Notice: Array to string conversion in

From Dev

Need help fixing my implementation of RK4

From Dev

Need help fixing a vba Error '1004' after editing a section of code

From Dev

Need help fixing my error on Notice: Array to string conversion in

From Dev

Need help fixing CSS for a GTK theme for Gnome Shell

From Dev

Need help fixing top bar inside another div with scroll

From Dev

Need help fixing my isotope.js [jquery and html code]

From Dev

Need help fixing broken nautilus directory links on launcher

From Dev

Need help fixing a random sentence generator that uses for loops and list's

From Dev

Need some help in fixing the Spark streaming dependency (Scala sbt)

From Dev

Need Help fixing incorrect output to console window.

From Dev

Need help LOOPING function

From Dev

Need help for clicking a function

From Dev

Need help in FORMAT function

From Dev

Need help for clicking a function

Related Related

HotTag

Archive