我无法理解这个Python程序中的错误
我找不到其中的错误。它给出的错误是您尚未创建函数life_left或无法找到它 你想让他们打多少次?五, 错误:我无法理解这个Python程序中的错误,python,python-3.x,Python,Python 3.x,我找不到其中的错误。它给出的错误是您尚未创建函数life_left或无法找到它 你想让他们打多少次?五, 错误: 回溯(最近一次呼叫最后一次): 文件“main.py”,第42行,在 开始(7) 文件“main.py”,第39行,开头 arr[编号]。攻击(枪支[枪支编号]) 攻击中第19行的文件“main.py” 离开的生活(自我生活) NameError:未定义名称“life_left” 代码: 随机导入 阶级敌人: 定义初始化(自): 自我生活=20 def攻击(自我攻击、枪攻击): i
回溯(最近一次呼叫最后一次):
文件“main.py”,第42行,在
开始(7)
文件“main.py”,第39行,开头
arr[编号]。攻击(枪支[枪支编号])
攻击中第19行的文件“main.py”
离开的生活(自我生活)
NameError:未定义名称“life_left”
代码:
随机导入
阶级敌人:
定义初始化(自):
自我生活=20
def攻击(自我攻击、枪攻击):
if(self.life您必须在方法定义和调用类内的方法时使用self
。以下是固定代码:
import random
class Enemy:
def __init__(self):
self.life = 20
def attack(self,gun):
if (self.life <= 0):
print("Dead")
else:
if gun is "AK47":
self.life = self.life - 5
self.life_left(self.life)
elif gun is "Magnum":
self.life = self.life - 4
self.life_left(self.life)
else:
self.life = self.life - 1
self.life_left(self.life)
def life_left(self,life):
print ("Life : " , str(life))
def start(players):
a = 1
arr = []
while a <= players:
arr.append("player" + str(a))
arr[a-1] = Enemy()
#print (arr[a-1])
a += 1
enemy = int(input("How many times do you want them to fight ? "))
fight = 1
guns = ["AK47" , "Magnum" , "other"]
while fight <= enemy:
number = random.randint(0,players-1)
guns_number = random.randint(0,len(guns)-1)
arr[number].attack(guns[guns_number])
fight += 1
start(7)
随机导入
阶级敌人:
定义初始化(自):
自我生活=20
def攻击(自我攻击、枪攻击):
如果(self.life)你指的是@match所说的self.life\u left
,在life\u left
定义-def life\u left(self,life)
…你还应该在life\u left
中使用self.life
(并且没有参数)当我们每次调用同一类的方法时,我们是否必须做这样的事情,或者我们是否可以为这个解决方案提供一个替代方法。self
类似于其他oop语言中的this
。请看
import random
class Enemy:
def __init__(self):
self.life = 20
def attack(self,gun):
if (self.life <= 0):
print("Dead")
else:
if gun is "AK47":
self.life = self.life - 5
life_left(self.life)
elif gun is "Magnum":
self.life = self.life - 4
life_left(self.life)
else:
self.life = self.life - 1
life_left(self.life)
def life_left(life):
print ("Life : " , str(life))
def start(players):
a = 1
arr = []
while a <= players:
arr.append("player" + str(a))
arr[a-1] = Enemy()
#print (arr[a-1])
a += 1
enemy = int(input("How many times do you want them to fight ? "))
fight = 1
guns = ["AK47" , "Magnum" , "other"]
while fight <= enemy:
number = random.randint(0,players-1)
guns_number = random.randint(0,len(guns)-1)
arr[number].attack(guns[guns_number])
fight += 1
start(7)
import random
class Enemy:
def __init__(self):
self.life = 20
def attack(self,gun):
if (self.life <= 0):
print("Dead")
else:
if gun is "AK47":
self.life = self.life - 5
self.life_left(self.life)
elif gun is "Magnum":
self.life = self.life - 4
self.life_left(self.life)
else:
self.life = self.life - 1
self.life_left(self.life)
def life_left(self,life):
print ("Life : " , str(life))
def start(players):
a = 1
arr = []
while a <= players:
arr.append("player" + str(a))
arr[a-1] = Enemy()
#print (arr[a-1])
a += 1
enemy = int(input("How many times do you want them to fight ? "))
fight = 1
guns = ["AK47" , "Magnum" , "other"]
while fight <= enemy:
number = random.randint(0,players-1)
guns_number = random.randint(0,len(guns)-1)
arr[number].attack(guns[guns_number])
fight += 1
start(7)