저는 'python'에 익숙하지 않고 클래스와 객체를 사용해 보았습니다.
클래스를 이해하기 위해 작은 코드를 사용해 보았습니다.
이 코드는 기본적으로 Hero
을 health
(나는 100을주었습니다)으로 만듭니다. 데미지로 3 번의 공격이 있습니다. 코드는 입력을 묻습니다 "어떻게 공격하겠습니까?" 플레이어에서. att
사전에 주어진 공격에 대해 프로그램은 공격으로 인한 손상으로 원래의 건강을 뺀 플레이어의 새로운 health
을 반환해야합니다.Python의 클래스와 상속
내 문제는 사용자의 입력을받은 후 플레이어의 새로운 health
을 반환하는 방법을 이해할 수 없다는 것입니다. 내가 얻는 오류는 다음과 같습니다 :
AttributeError: 'Input' object has no attribute 'health'.
제발 누군가가 구문을 도울 수 있습니다.
class Hero(object):
def __init__(self,name):
self.name=name
def stats(self,health):
self.health=health
def attack(self,kick,punch,kamehameha):
self.punch=punch
self.kick=kick
self.kamehameha=kamehameha
att={"kick":20,"punch":10,"kamehameha":50}
#taking input from the player
class Input(Hero):
def __init__(self,a):
a=raw_input("how will you attack?:")
self.a=a
def new(self):
for a in att[key]:
return self.health - att[key]
#create a player
player1=Hero("goku")
player1.stats(100)
print player1.name
print player1.health
x=Input(att)
print x.new()