简体   繁体   English

Python类无法执行我的功能

[英]Python Class Won't execute my function

So I made this code a little bit ago, but it won't use the check_level command or the award command when called: 因此,我稍早编写了此代码,但在调用时将不使用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!')

This: 这个:

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

Should be: 应该:

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

The self param is implied. 隐含了self参数。

EDIT: actually any of your RPG.*(self) functions should be self.*() -- I see a few of them sprinkled throughout the class. 编辑:实际上,您的任何RPG.*(self)函数都应该是self.*() -我看到其中的一些在整个课程中散布。

Samsquanch is correct about changing all your RPG.(self) method calls to self.*() . Samsquanch关于将所有RPG.(self)方法调用更改为self.*()是正确的。 When you call a method, python will always pass the instance object as the first parameter. 调用方法时,python始终会将实例对象作为第一个参数传递。 That way you can avoid having to explicitly call the method as you were doing previously. 这样,您可以避免像以前一样显式调用该方法。 You can check out this link for some more detailed information on self . 您可以查看此链接以获取有关self更多详细信息。

I tested out your code and it looks like your problem lies within check_enemy() . 我测试了您的代码,看来您的问题出在check_enemy() You need to change self.set == True to self.set = True . 您需要将self.set == True更改为self.set = True

The new method should look like this 新方法应如下所示

def check_enemy(self):
    if self.eh == 0:
        print('You Win')
        self.set = True
        self.set_basic()
    else:
        print('')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM