简体   繁体   English

Python:为什么说我的方法没有明确定义?

[英]Python: Why is it saying my method isn't defined when it clearly is?

class GameMech:
    def __init__(self ,aCharacter):
        print("A battle is starting")
    def getMP(self, aCharacter):
        return aCharacter.mp
    def getHP(aCharacter):
        return aCharacter.hp
    def getAtk(aCharacter):
        return aCharacter.atk
    def getDef(aCharacter):
        return aCharacter.defense
    def usePotion(aCharacter):
        aCharacter.hp += 100
        return aCharacter.hp
    def useSpecial(self, aCharacter, target):
        if aCharacter.mp >= 100:
            target.hp -= 45

    def dead(self, aCharacter):
        return aCharacter.name +" is now dead"
    def attack(self, aCharacter, target):
        if target.hp - (aCharacter.atk/aCharacter.defense) <= 0:
            dead(target)
        else:
            target.hp - aCharacter.atk/aCharacter.defense
            print(getHP(target))
    def guard(self, aCharacter):
        print(aCharacter + "was unharmed")
        if aCharacter.hp <= 50:
            retaliation(aCharacter, target)
    def retaliation(self ,aCharacter, target):
        target.hp - (aCharacter.atk/10)
        print(getHP(target))

    def battleMenu(aNumber, self):
        if aNumber == 1:
            attack(aCharacter, target)
        if aNumber == 2:
            guard(aCharacter)
            print(aCharacter + " was unharmed!")
        if aNumber == 3:
            useSpecial(aCharacter, target)
            print(getHP(target))
        if aNumber == 4:
            heal = useItem(aCharacter)
            print(heal)

    def myTurn(self):
        print("ATTACK")
        print("GUARD")
        print("SPECIAL")
        print("ITEM")
        aNumber = int(input("What would you like to do? "))
        battleMenu(aNumber)

    def oppTurn(self):
        print("It is the opponent's turn")
        randomNum = random.randint(1,4)
        battleMenu(randomNum)

a few notes, obviously due to this formatting you can't tell that all my methods are actually under the class, but they are.一些注意事项,显然由于这种格式,您无法判断我的所有方法实际上都在类下,但它们确实存在。 you can ignore the first few parts, the part im focusing on is battle menu.你可以忽略前几个部分,我关注的部分是战斗菜单。 So i'm creating a little text game as a project for myself, pure leisure nothing academic or for a job.所以我正在创建一个小文本游戏作为我自己的一个项目,纯粹的休闲没有学术或工作。 Just a little game for myself to help me jog my memory in regards to python.只是我自己的一个小游戏,可以帮助我在 python 方面慢跑我的记忆。 So that's where the gut of my game is, the character class is an another file and there's nothing important in there just the attributes of character (hp, mp, atk, def) here's the main所以这就是我游戏的核心所在,角色类是另一个文件,其中没有什么重要的,只是角色的属性(hp,mp,atk,def)这里是主要的

import random
import character
import battle



#create a battle menu
#and a character value check menu
#add buttons




def main():
char1 = character.Character("Charlie", 5000, 2000, 1500, 750)
char2 = character.Character("Mark", 2000, 4000, 2000, 900)

newGame = battle.GameMech(char1)
while char1.hp != 0 or char2.hp != 0:
    newGame.myTurn()
    newGame.oppTurn()

main() (not sure why it's not including the definition of my main in the code snippet) Any how I'm getting this error saying "name 'battleMenu' is not defined". main()(不知道为什么它没有在代码片段中包含我的 main 的定义)不管我是如何收到这个错误的,说“名称‘battleMenu’未定义”。 Name usually applies to variables correct?名称通常适用于变量是否正确? But battleMenu is a method is there a reason i'm getting this error?但是 BattleMenu 是一种方法,我收到此错误有什么原因吗?

Traceback (most recent call last):
File "E:\PythonGame\mainGame.py", line 22, in <module>
  main()
File "E:\PythonGame\mainGame.py", line 20, in main
  newGame.myTurn()
File "E:\PythonGame\battle.py", line 55, in myTurn
  battleMenu(self,aNumber)
NameError: name 'battleMenu' is not defined

To call a method, you need to mention the instance you're calling it on, so you need to do self.battleMenu() .要调用一个方法,您需要提及调用它的实例,因此您需要执行self.battleMenu() You need to make similar changes for all of the method calls in your code.您需要对代码中的所有方法调用进行类似的更改。 You also need to add a self argument to all your methods so that these changes will work.您还需要为所有方法添加一个self参数,以便这些更改生效。 You also need to read the Python tutorial to get an understanding of Python basics.您还需要阅读Python 教程以了解 Python 基础知识。

self.battleMenu(...)  # note the "self" here

You are calling it wrongly, def battleMenu(aNumber): , your function is not defined as an instance function, neither as a static method, If you want a static function you should first annotate the method as a static one , by using @staticmethod annotation.你错误地调用了它, def battleMenu(aNumber): ,你的函数没有定义为实例函数,也没有定义为静态方法,如果你想要一个静态函数,你应该首先使用@staticmethod将该方法注释为静态方法注解。

Then call your function as - GameMech.battleMenu(random) .然后将您的函数称为 - GameMech.battleMenu(random)

If you want it to be an instance function, change the definition to def battleMenu(self, aNumber): and then check.如果你希望它是一个实例函数,将定义更改为def battleMenu(self, aNumber):然后检查。

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

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