我是这个网站的新手,如果我做的荒唐或违反规定,但我有疑问,请对不起。
我是Python和编程新手。我正在学习Python,而我在运动时遇到了Error。我在这里搜索解决方案,但是大多数解决方案超出了我无法理解的水平。
请尝试以一种初学者可以理解的方式回答,谢谢。
这里是代码,我得到的错误是; 'AttributeError:'tuple'对象没有属性'print'
感谢您的帮助。
import random
class Enemy:
name = "Enemy"
health = 100
damage = 5
ammo = 20
def __init__(self,name,health,damage,ammo):
self.name = name
self.health = health
self.damage = damage
self.ammo = ammo
def properties(self):
print("Properties: ")
print("Name: ",self.name)
print("Health: ",self.health)
print("Damage: ",self.damage)
print("Ammo: ",self.ammo)
def attack(self):
print(self.name + " is attacking!")
ammo_spent = random.randrange(1,10)
print(str(ammo_spent) + " ammo spent.")
self.ammo -= ammo_spent
return (ammo_spent,self.damage)
def getattacked(self,ammo_spent,damage):
print ("I've been shot!")
self.health -= (ammo_spent * damage)
def is_ammo_depleted(self):
if (self.ammo <= 0):
print (self.name + "'s ammo depleted.")
return True
return False
def check(self):
if (self.health <= 0):
print("YOU DIED.")
Enemies = []
i = 0
while (i < 9):
randomhealth = random.randrange(125,300,25)
randomdamage = random.randrange(25,100,25)
randomammo = random.randrange(20,200,20)
new_enemy = ("Enemy" + str(i+1),randomhealth,randomdamage,randomammo)
Enemies.append(new_enemy)
i += 1
for Enemy in Enemies:
Enemy.properties()
答案 0 :(得分:2)
您需要创建您的类的实例:
Enemies = []
i = 0
while (i < 9):
randomhealth = random.randrange(125,300,25)
randomdamage = random.randrange(25,100,25)
randomammo = random.randrange(20,200,20)
# create an Enemy - not a tuple of values
new_enemy = Enemy( "Enemy {}".format(i), randomhealth, randomdamage, randomammo)
Enemies.append(new_enemy)
i += 1
for Enemy in Enemies:
Enemy.properties()
如果您在类中创建__str__(self)
方法,则可以“告诉” python如何打印类的实例:
class Enemy:
# snipped what you already had
# is used if you print(instance)
def __str__(self):
return """Properties:
Name: {}
Health: {}
Damage: {}
Ammo: {}""".format(self.name, self.health, self.damage, self.ammo)
# is used by lists if you print a whole list
def __repr__(self):
return str(self)
在此处了解有关__str__
的信息:python __str__ for an object
答案 1 :(得分:0)
好的,首先,别像
i = 0
while (i < 9):
i++
// bad
使用
for i in range(9):
// good
相反。
第二次,
new_enemy = ("Enemy" + str(i+1),randomhealth,randomdamage,randomammo)
Enemies.append(new_enemy)
您要将元组附加到“实体”列表。您怎么可能期望Tuple具有Entity类的属性?
所以您要使用的是
Enemies = []
for i in range(9):
randomhealth = random.randrange(125,300,25)
randomdamage = random.randrange(25,100,25)
randomammo = random.randrange(20,200,20)
new_enemy = Enemy("Enemy" + str(i+1), randomhealth, randomdamage, randomammo)
Enemies.append(new_enemy)
for Enemy in Enemies:
Enemy.print()
接下来,在最后一个for循环中,您将类名Enemy用作循环中的变量。糟糕的主意,但幸运的是,您只能将其转换为小写。 那么,为什么仍然说您的敌人班级没有印刷会员呢?因为尚未定义,所以您将方法称为“属性”。
for enemy in Enemies:
enemy.properties()
它有效。 但是,由于我们使用的是python,因此可以大大简化此代码。
最终结果:
import random
class Enemy:
def __init__(self,name,health,damage,ammo):
self.name = name
self.health = health
self.damage = damage
self.ammo = ammo
def __str__(self):
return "Properties:\nName: {}\nHealth: {}\nDamage: {}\nAmmo: {}".format(
self.name, self.health, self.damage, self.ammo)
#your other methods
Enemies = [Enemy(name="Enemy" + str(i),
health=random.randrange(125, 300, 25),
damage=random.randrange(25, 100, 25),
ammo=random.randrange(20, 200, 20))
for i in range(1, 10)]
for enemy in Enemies:
print(enemy)