如何将stat从类调用到函数Python中

2024-10-04 11:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力创建一个健康/愤怒(变量)下降时,让用户战斗一个骑士。我定义了一个名为kingsmen的类,这个类非常简单,适用于每个骑士,但是我一直被告知我没有正确地调用这个类。我不知道该怎么解释。下面是类和骑士的代码,错误消息将在底部。一些打印间距是奇怪的(那些与“”),因为该网站是困难的。 谢谢您!你知道吗

班级:

class kingsmen:                                   

    def __init__(self):
        self.stats = {}

    def attributes(self):                                  
        health=15
        anger=7
        self.stats["health"] = 15                     #storing default health count
       self.stats["anger"] = 7
       return self.stats
   def warning(self):                                #players will see this message when meeting a knight
       print ('A kingsmen! Prepare for battle!')
   def noise(self):
       print ("H e y ,  i t ' s  a  v i l l a g e r !")        #have these attributes later. 
   def death(self):
       if healh == 0:
           print ('''DEFEATED''')

骑士:

def rknightOne(kingsmen):           #r stands for random, he will be one of 5 random villains you can face
    rknightOne=kingsmen()
    #while health in kingsmen.stats()!= 0 and anger != 0:
    if action == 'attack':
        for health in rknightOne.attributes:
            health=health-1
            print health
        print_slowly ('I did not feel a thing. Ha ha ha!')
        healthDrop(user)
    elif action == 'mercy':
        for anger in rknightOne.attributes:
            anger=anger-1
        print_slowly ('Huh. I feel something warm inside.')
    elif action=='run':
        print_slowly ('Not so fast!')
    else:
        print_slowly ("Jeez this guy is looney. I CAN'T UNDERSTAND YOU!")

错误:

Traceback (most recent call last):
File "C:/Python27/REDEMPTION/Redemption Game V1.py", line 678, in <module>
    print randomKnight()
File "C:/Python27/REDEMPTION/Redemption Game V1.py", line 440, in randomKnight
    print random.choice([rknightOne(kingsmen), rknightTwo(kingsmen), rknightThree(kingsmen), rknightFour(kingsmen), rknightFive(kingsmen)])
File "C:/Python27/REDEMPTION/Redemption Game V1.py", line 178, in rknightOne
for health in rknightOne.attributes:
TypeError: 'instancemethod' object is not iterable

它所说的随机函数只是在5个骑士之间循环,可能会弹出。以下是代码,以防有帮助:

def randomKnight():
    print random.choice([rknightOne(kingsmen), rknightTwo(kingsmen), rknightThree(kingsmen), rknightFour(kingsmen), rknightFive(kingsmen)])

Tags: inselffordefstatsactionrandomattributes
2条回答

改变这个

for health in kingsmen.stats():

使用以下语句之一

for health in kingsmen.stats: # if you don't want to call the method
for health in kingsmen.stats(some_argument): # or if you want to call the method provide arguments

出现此错误的原因是您已经定义了

self.stats = {}

以及

def stats(self)

在国王的课堂上,所以你很困惑。你知道吗

有了最新版本的代码,您现在要做的事情就一目了然了。您看到的错误消息告诉您不能迭代实例方法。这正是你想做的。你在试着绕过去kingsmens.attributes属性这是你们班的一个方法。你不能那样做。您的方法实际上是返回一个字典,执行该方法的结果是您可以迭代的。所以您实际上想要迭代调用attributes()的结果。然而,在此之前还有一个问题需要澄清,我将在下面解释。你知道吗

为了能够正确地调用属性,必须首先实例化类(documentation on classes and objects in Python)。你知道吗

您所做的不是执行该方法,而且,也不是实例化类以便能够访问和执行该方法。你知道吗

要解决此问题,请点击此处:

for health in kingsmen.attributes:

首先需要实例化类,然后使用类中的对象调用attributes方法。实现的attributes方法的返回将返回您试图迭代的字典。因此,您的代码应该如下所示:

my_kingsmen = kinsgmen()
for health in my_kinsgmen.attributes():

相关问题 更多 >