" AttributeError: 'list' object has no attribute 'replace " Error. Fix: Use a basic for loop to replace all letters

Question:

Its a hangman game and I know it isn’t done, but I’m stuck on this error
I’m getting the error AttributeError: 'list' object has no attribute 'replace'

the code:

import random
word_list_easy = ["word", "apple", "banana", "cheese"]
difficulty = str(input("Please enter difficulty: Easy, Medium or Hard. "))
difficulty = difficulty.upper()
empty = ("")
if difficulty == ("EASY"):
    maximum = len(word_list_easy)
    maximum = maximum - 1
    #makes a random variable that cant excede the length of the list
    random = random.randint(0,maximum)
    random_word = word_list_easy[random]
    #chooses a random word in the list
    word_length = len(random_word)
    guessed_letters = ['_'] * word_length
    print('  ' + ' '.join (guessed_letters))
    print("There are ", word_length ,"letters in the word")
    #sets how many tries you get. (later will make graphical)
    tries = 5
    while tries >= 1:
        #will repeat untill out of tries
        guessed_letter = input("Enter your guess of the letter ")
        while len(guessed_letter) > 1:
                  guessed_letter = input("Enter one letter that you think is in the word ")
        while (guessed_letter) == (""):
            guessed_letter = input("Please enter a letter ")
                  #if the letter is in the word
        new_random_word = random_word.replace(guessed_letter, empty)
        if (guessed_letter) in (random_word):
            print("You guessed correctly")
            #find the length of the new word to repeat the "_" lines for length of new word
            for index in range (word_length):
                if guessed_letter == random_word [index]:
                    guessed_letters[index] = guessed_letter
            print (" " + " ".join (guessed_letters))
        else:
            print("Sorry but your guess was wrong ")
            tries = tries - 1
            print("You have",tries," tries left")
            print (guessed_letters)
        check_word = guessed_letters.replace(" ", "")
        if check_word == random_word:
            print("Congratulations, you managed to complete the word. Maybe try a higher difficulty next time ")

what does this mean and what am I doing wrong and how to fix it. The replace() method is From the end the third line.

Answers:

There’s nothing built-in method for replacement in lists, but it’s just a loop to do the replacement, check it, maybe worked for you:

check_word = [x.strip() for x in guessed_letters]
check_word = "".join(check_word)
Answered By: Signor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.