tromegatron
tromegatron

Reputation: 5

Replace strings in list B at indices using indices from list A

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())

Upvotes: 0

Views: 44

Answers (2)

Robert Price
Robert Price

Reputation: 641

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.

Upvotes: 0

bartius
bartius

Reputation: 202

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!

Upvotes: 1

Related Questions