Python 类不会执行我的函数

标签 python class python-3.x

所以我之前编写了这段代码,但在调用时它不会使用 check_level 命令或 Award 命令:

from random import randint

class RPG:
    #Defines parts of your character
    def __init__(self, character_name):
        self.chrn = character_name
        self.exp = 0 #Total Experince points
        self.level = 1 #Total Level
        self.h = 25 #Player Health
        self.expn = 10 #Experience Needed
        self.eh = 10 #Base Enemy Health
        self.set = False #Used to chest enemy status
        self.win = False #Used to check for win

    def set_basic(self): # Sets up the basic enemy.
        if self.eh == 0:
            self.eh = 10

    def check_level(self): # Checks to see if you have enough EXP to level up.
        if self.exp >= self.expn:
            self.level = self.level + 1
            self.exp = 0
            self.expn = self.level * 10
            print('Level has increased to {0}'.format(self.level))


    def check_enemy(self): # Checks to see if enemy's health is equal to 0.
        if self.eh == 0:
            print('You Win')
            self.set == True
            RPG.set_basic(self)
        else:
            print('')

    def award(self): # Awards EXP if enemy is dead.
        if self.set == True:
            self.exp = self.exp + 5
            print ('You gained 5 EXP!')

    def attack(self): #The main character's attack.
        x = randint(1,100)
        if x > 20:
            y = randint(1, self.level * 3)
            self.eh = self.eh - y
            final = ('Enemy took {0} damage'.format(y))
            print(final)
            if self.eh < 0:
                self.eh = 0
            print('Enemy has {0} health left'.format(self.eh))
            print('')
            RPG.check_enemy(self)
            RPG.award(self)
            RPG.check_level(self)
        else:
            print('Miss!')

最佳答案

这个:

RPG.check_enemy(self)
RPG.award(self)
RPG.check_level(self)

应该是:

self.check_enemy()
self.award()
self.check_level()

self 参数是隐含的。

编辑:实际上,您的任何 RPG.*(self) 函数都应该是 self.*() ——我看到了一些其中遍布全类。

关于Python 类不会执行我的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981789/

相关文章:

python - 并行 scipy.sparse 逐元素乘法

Python/Flask mysql 游标 : Why it doesn't work?

Python - 为什么 __str__ 在调用时想要返回一些东西?我在 __str__ 中有打印函数

python - 如何将字符串转换为嵌套列表,元素以逗号分隔

python-3.x - 在PyCharm中导入opencv python包时出现问题

Python 在列表理解中引发错误(或更好的替代方案)

Python - Discord.py - wait_for() 我的 Cog 的意外关键字

python - 在 UltimateListCtrl 中排序项目

python - 具有不可迭代对象的 python 类中的错误

c++在子类的父类中设置指针变量并在父类中使用它