0

Hopefully the way I worded the question makes sense. I'm creating a simple version of the game "Hangman." I want the user to be able to see where a correct guess is positioned in the word. I figured I can accomplish this by using two lists. List A contains the word the user is trying to guess. List B contains correct guesses and their position in the word. I know how to find the position of the letters in list A, but I can't seem to figure out how to place the correct guesses at their corresponding indices in list B.

I found the indices of list A using

print([i for i, x in enumerate(word) if x == answer])

Now the problem is that I can't figure out how to use their position to correctly insert correct guesses into their corresponding indices. Words with more than one instance of a given letter make this kinda hard for me.

def get_word():
    try:
        get_answer = list(input('Please type a word: '))
    except TypeError:
        print('Words only please!')
    return get_answer
def check_guess(word):
    print('Welcome to hangman!')
    missing_char = '_'
    progress_list = []
    guess_list = []
    print('Your word is', +len(word), 'characters long. You will get',+len(word)+3, 'guesses. Good luck!\n')
    guesses = len(word) + 3
    for j in word:
        progress_list.append(missing_char)
    for i in range(guesses):
        while guesses > 0:
            answer = input('Enter a letter: ')
            print()
            guesses -= 1
            if answer in word:
                progress_list.insert(0, answer)
                guess_list.append(answer)
                print("Correct! The letter '", answer, "' appears", word.count(answer),"time(s)!")
                print()
                print('Here are all of your guesses so far: ',guess_list)
                print()
                print('Here is what is left', progress_list)
                print()
                print('You have', +guesses, 'guesses left! ')
                print()
            else:
                print('Sorry, that letter does not appear in the word')
                print()
                guess_list.append(answer)
                print('Here are all of your guesses so far: ',guess_list)
                print()
                print('Here is what is left', progress_list)
                print()
                print('You have', +guesses, 'guesses left! ')
                print()
check_guess(get_word())
1

2 Answers 2

1

My suggestion would be to use a dictionary. Let me elaborate.

In hangman, when a user guesses a letter and it's correct, then every instance of that letter is illuminated.

So, instead of finding the indices of list A like you did here:

print([i for i, x in enumerate(word) if x == answer])

store each letter in a dictionary with the letters as the keys, and the values as an array of indices.

Then, you can simply access that dictionary at the corresponding letter and get all of the indices for only that letter. Then use those indices in list B.

I hope this helps! Feel free to ask for clarification!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I’ll try this when I get home
0

Instead of the progress_list.insert(0, answer) line, you can do:

for i, x in enumerate(word):
    if x == answer:
        progress_list[i] = x

It's the same idea as your list comprehension, but writing it as a loop lets you make the changes to progress_list at the same time.

1 Comment

Great suggestion, thank you! That did the trick quite nicely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.