首页 > 解决方案 > TypeError:“列表”对象不可调用 - 类编码

问题描述

我正在尝试在 python 中编写一个类。我认为我的问题的答案可能很简单,但我是新来的代码,这里是代码:

实际代码

一旦运行

我没有看到任何问题,我错过了一些简单的事情吗?

标签: python-3.xcomputer-science

解决方案


方法名和列表名都是动作。当其初始化的 self.actions(method) 被 self.actions(list) 替换时。列表不可调用

class Troll:
    def __init__(self):
        self.name = ''
        self.phrases = ['Pass the ball','SHOOT']
        self.available_actions = ['passes the ball','shoots']
    def speak(self):
        print(self.name, "shouts", random.choice(self.phrases))
    def actions(self):
        print(self.name,"decides to", self.available_actions)

推荐阅读