![](/img/trans.png)
[英]Python: AttributeError: 'tuple' object has no attribute 'read'
[英]Python AttributeError: 'tuple' object has no attribute 'print'
我是本網站的新手,如果我做的荒唐或違反規定,對不起,但我有一個問題。
我是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()
您需要創建類的實例:
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
好吧,首先,不要像
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.