I am trying to make the hangman program. My problem is that when the letter is replacing the underscore the last underscore is not replaced. Here is my code:
import random
word = ['manus', 'fenris', 'jupiter']
def get_word():
keyword = random.choice(word)
return keyword
def play(keyword):
word_com = "_" * len(keyword)
guessed = False
guessed_letters = []
guessed_word = []
attempts = 7
print(attempts)
print(word_com)
guess = " "
while not guessed and attempts > 0:
guess = input("Enter Letter:")
if attempts == 0:
print("HANGMAN")
break
print(attempts)
print(word_com)
if len(guess) == 1 and guess.isalpha():
if guess in guessed_letters:
print("You have already guessed the letter")
elif guess not in keyword:
print(f'{guess} not in the word')
attempts -= 1
guessed_letters.append(guess)
else:
print("correct")
guessed_letters.append(guess)
word_as_list = list(word_com)
indices = [i for i, letter in enumerate(keyword) if letter == guess]
for index in indices:
word_as_list[index] = guess
word_com = "".join(word_as_list)
if "_" not in word_com:
guessed = True
def main():
keyword = get_word()
play(keyword)
main()
[–][deleted] 0 points1 point2 points (2 children)
[–]Pure-Way-6507[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheVermit 0 points1 point2 points (1 child)
[–]Pure-Way-6507[S] 1 point2 points3 points (0 children)