AttributeError: 'Game' object has no attribute 'tries_left'

Question:

So i am making a chat bot with python and i have tried alot of different methods on how to fix the problem any way heres the code

class Game:
    def __init__(self, word):
        self.guessed_letters = []
        self.word = word
        # Define tries_left and initialize it to 8
        def tries_left():
            self.tries_left = 8
    
    def play(self):
        dashed_word = ""
        while dashed_word != self.word:
            send_message("Guess a letter")
            guess = read_message()
            self.guessed_letters.append(guess)
            
            # Add if to check if letter is not in the word,
            # then subtract 1 from tries_left
            if guess not in self.word:
                send_message("Your guess is wrong")
                self.tries_left -= 1
                
            dashed_word = ""
            for char in self.word:
                if char in self.guessed_letters:
                    dashed_word += char
                else:
                    dashed_word += "_  "
            send_message(dashed_word)
            send_message(self.guessed_letters)
            # Send tries_left after each guess. PROBLEM HERE 
            send_message("You have " + str(self.tries_left) + " tries left")
        
        send_message("Congratulations! You have guessed the word")   
            

game = Game("snowman")
game.play()

I need a answer quick

Asked By: Muhammad Sibtain

||

Answers:

Just change the init function to:

    def __init__(self, word):
        self.guessed_letters = []
        self.word = word
        # Define tries_left and initialize it to 8
        self.tries_left = 8

In your original code, you defined a function (tries_left) that assigns the member, but this function has never called.

Answered By: saart
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.