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.