[英]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.