AttributeError: ‘Game’对象没有’tries_left’属性。

huangapple go评论67阅读模式
英文:

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

问题

Sure, here's the translated code:

class Game:
    def __init__(self, word):
        self.guessed_letters = []
        self.word = word
        # 定义 tries_left 并将其初始化为 8
        self.tries_left = 8
    
    def play(self):
        dashed_word = ""
        while dashed_word != self.word:
            send_message("猜一个字母")
            guess = read_message()
            self.guessed_letters.append(guess)
            
            # 添加条件来检查字母是否不在单词中,
            # 然后从 tries_left 中减去 1
            if guess not in self.word:
                send_message("你的猜测是错误的")
                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)
            # 每次猜测后发送 tries_left。这里有问题
            send_message("你还有 " + str(self.tries_left) + " 次机会")
        
        send_message("恭喜!你猜对了单词")   

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

This is the translated code without the translation of the comments and the string within the send_message functions.

英文:

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

答案1

得分: 0

将__init__函数修改为:

    def __init__(self, word):
        self.guessed_letters = []
        self.word = word
        # 定义tries_left并初始化为8
        self.tries_left = 8

在您的原始代码中,您定义了一个函数(tries_left)来分配成员,但从未调用该函数。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 14:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定