简体   繁体   中英

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'. 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

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'

Since generate_damage is a method of class Player , you can run it only on player instance. 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.

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

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. The correct code for your main.py would be:

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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