需要帮助调用方法

时间:2017-10-29 19:42:19

标签: python class methods

嘿,我是Python和编程的新手。我正在为学校做一个项目,我必须让NPC说出我给他的一些随机代码。我的教授为我们提供了一些代码,我们必须自己填写几行代码。但是,我很难让程序在windows powershell上显示任何文本。我看过这个网站上的很多教程,据他们说,我正在做的一切正确。任何人都可以告诉我我做错了什么以及为什么这是错的?

from random import randint
class NPC (object):
def __init__(self,name, phrases):
    self.__name = name
    self.__phrases = ['Today is Tuesday', 'Today is Wednesday', 'Today is Thursday', 'Today is Friday']

def getDescription(self):
    return self.__description

def add_phrase(self, phrase):
    return "Howdy"

def getPhrases(self):
    return self.__phrases

def speak(self):
    for i in self.__phrases:
        return randint

def __str__(self):
    return "Good day, I am " + self.__name

c = NPC()
c.speak()

我们是作为模板提供的。

到目前为止我所拥有的是

.name

另外,我应该在speak功能下放什么,以便随机返回其中一个短语?

1 个答案:

答案 0 :(得分:0)

试试这个,看看我做的改变:

from random import randint

class NPC (object):

    def __init__(self,name, phrases):
        self.__name = name
        self.__phrases = phrases

    def getDescription(self):
        return self.__description

    def add_phrase(self, phrase):
        return "Howdy"

    def getPhrases(self):
        return self.__phrases

    def speak(self):
        rand = randint(0,len(self.__phrases)) #Selecting a random phrase
        print(self.__phrases[rand])


    def __str__(self):
        return "Good day, I am " + self.__name


name = "Stranger"
# Initializing phrase list and then passing it as an argument to the constructor.
phrases = ['Today is Tuesday', 'Today is Wednesday', 'Today is Thursday', 'Today is Friday']

c = NPC(name, phrases)
c.speak()

其中一项产出是:

Today is Wednesday