简体   繁体   English

如何修复 Python 中的“TypeError”?

[英]How do I fix a “TypeError” in Python?

I'm totally new to Python just started learning a few days ago, and I tried to make a simple RPG code, but I have recently ran into a hiccup, it keep returning me TypeError: generate_damage() missing 1 required positional argument: 'self'.我对 Python 完全陌生,几天前才开始学习,我试图制作一个简单的 RPG 代码,但我最近遇到了一个小问题,它一直返回我 TypeError: generate_damage() missing 1 required positional argument: '自己'。 Does anyone know what's going on?有谁知道发生了什么?

Main.py主文件

from Classes.game import player, bcolors

magic = [{"name": "Tackle", "cost": 1, "dmg": 4},
         {"name": "Ember", "cost": 3, "dmg": 6},
         {"name": "Leech", "cost": 4, "hp": +5, "dmg": 4},
         {"name": "Explosion", "cost": 5, "hp": -10, "dmg": 30}]

player(100, 20, 5, 10,magic)

print(player.generate_damage())

Game.py游戏.py

import random 

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

class player:
    def __init__(self, hp, mp, atk, df, mag):
        self.maxhp = hp
        self.hp = hp
        self.maxmp = mp
        self.mp = mp
        self.atkw = atk - 10
        self.atks = atk + 10
        self.df = df
        self.mag = mag
        self.actions = ["Attack", "Magic"]

    def generate_damage(self):
        return random.randrange(self.atkw, self.atks)

I am having problems with the:我遇到以下问题:

print(player.generate_damage())

As it keeps returning TypeError: generate_damage() missing 1 required positional argument: 'self'因为它不断返回 TypeError: generate_damage() missing 1 required positional argument: 'self'

Since generate_damage is a method of class Player , you can run it only on player instance.由于generate_damage是 class Player的一种方法,因此您只能在播放器实例上运行它。 Try this:尝试这个:

p = Player(100, 20, 5, 10, magic) # init object p of class player

print(p.generate_damage())

You need to create an instance of player first.您需要先创建一个player实例。

class Player:   # Note how I have changed the class to have an upper case P
    def __init__(self, hp, mp, atk, df, mag):
        self.maxhp = hp
        self.hp = hp
        self.maxmp = mp
        self.mp = mp
        self.atkw = atk - 10
        self.atks = atk + 10
        self.df = df
        self.mag = mag
        self.actions = ["Attack", "Magic"]

    def generate_damage(self):
        return random.randrange(self.atkw, self.atks)

player = Player(100, 20, 5, 10,magic)
print(player.generate_damage())

You must instanlize a class, to use it's class method您必须实例化 class,才能使用它的 class 方法

magic = [{"name": "Tackle", "cost": 1, "dmg": 4},
         {"name": "Ember", "cost": 3, "dmg": 6},
         {"name": "Leech", "cost": 4, "hp": +5, "dmg": 4},
         {"name": "Explosion", "cost": 5, "hp": -10, "dmg": 30}]

p = player(100, 20, 5, 10,magic)

print(p.generate_damage())

You are calling an instance method with the class name, you need to create an instance of that player class then call the instance method.您正在调用名称为 class 的实例方法,您需要创建该播放器 class 的实例,然后调用该实例方法。 The correct code for your main.py would be:您的main.py的正确代码是:

from game import player, bcolors

magic = [{"name": "Tackle", "cost": 1, "dmg": 4},
         {"name": "Ember", "cost": 3, "dmg": 6},
         {"name": "Leech", "cost": 4, "hp": +5, "dmg": 4},
         {"name": "Explosion", "cost": 5, "hp": -10, "dmg": 30}]

p = player(100, 20, 5, 10,magic)

print(p.generate_damage())

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

相关问题 如何修复 Python 中的 TypeError - How do I fix TypeError in Python 如何在python中修复TypeError - How to fix a TypeError in python 如何在python 3.3中修复此错误? TypeError:不可排序的类型:str()&lt;int() - how do I fix this error in python 3.3? TypeError: unorderable types: str() < int() 如何在 Python 中修复关于非整数和浮点数的这个 TypeError? - How do I fix this TypeError in Python concerning non-int and floats? 如何修复我的 python Yahoo Finance Webscrapper 中的类型错误、解析和所有其他错误 - How do I fix TypeError, Parsing, and all other errors in my python Yahoo Finance Webscrapper 如何修复此错误:TypeError: unsupported operand type(s) for *: 'float' and 'function' in python - How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python 你如何用 python 修复 - TypeError: must be str, nor int -? - How do you fix with python - TypeError: must be str, nor int -? 我收到一个类型错误。 我如何解决它? - I'm getting a TypeError. How do I fix it? 我有一个 python TypeError,我不知道如何修复它 - I have a python TypeError and I don't know how to fix it 如何修复“ TypeError:&#39;模块&#39;对象不可调用”? - How do i fix “TypeError: 'module' object is not callable”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM