0

In this code, the task is to write a function that picks a random word from a list of words from the SOWPODS.txt dictionary. Each line in the file contains a single word. This line of code place[random_word.index(ask)] = ask assigning the value of ask to place using index function, but the problem is the index function is showing the first index of item in list random_word for example the random list throws ['A', 'N', 'T', 'I', 'E', 'N', 'T'] after user input it will assign the value like ['A', 'N', 'T', 'I', 'E', ' ', ' '] i.e. this line of code is not assigning the duplicate items. P.S. I am trying to make a simple Hangman Game

import random
random_word_from_file = random.choice(open('sowpods.txt', 'r').read().split())
random_word = []

for letter in random_word_from_file:
    random_word.append(letter)

print(random_word, len(random_word))
print("\nWelcome to Hangman Game!\n")
print('Secret Word:  ',' '.join(random_word))
place = []
for i in range(len(random_word)):
    place.append(' ')
print(place)
chance = 0
while chance <= 6:
    ask = input("Enter letter: ").upper()
    if ask in random_word:
        place[random_word.index(ask)] = ask
        print(place)
    elif ask == 'exit':
        break
    elif ask not in random_word:
        chance += 1
    else:
        pass

I want to assign the whole letters that exits in random_word to place list.

RJT
  • 59
  • 7

2 Answers2

1

The list.index() function in Python will only return the index of the first position in which the query appears, meaning that if you have duplicate entries in the list (as you will with the word 'antient') you'll only get the index of the first occurrence of the letter each time.

To get an index for every occurrence of ask you can use a list comprehension, and then iterate over your list of indices. For example:

indices = [i for i, letter in enumerate(random_word) if letter == ask]
for index in indices:
    place[index] = ask

In other words:

  • Create a list containing the indices of random_word which matches ask
  • For each value in that list, replace that index of place with ask

I believe this will accomplish what you're looking for.

Duck Hunt Duo
  • 299
  • 1
  • 11
0

Because .index() gets the first occurrence, you will never get to the 2nd occurrence of a letter that way.

There's a lot of ways to go about this. What I did is create a tmp_word that is your random_word. Then I remove each letter occurrence as they happen, allowing you to still use .index()

tmp_word = random_word
while chance <= 6:
    ask = input("Enter letter: ").upper()
    if ask in tmp_word:
        place[tmp_word.index(ask)] = ask
        for i, word in enumerate(tmp_word):
            if word == ask:
                tmp_word[i] = ''
                break
        print(place)
        # Uncomment line below to see visualization of what's happening
        # print('_', tmp_word)
    elif ask == 'exit':
        break
    elif ask not in tmp_word:
        chance += 1
    else:
        pass
Matt M
  • 691
  • 2
  • 6
  • 17