我甚至不确定你能做到这一点。我试图从attack_moves得到整数值:战斗机:剑拳踢,希望我可以在攻击怪物后使用这些值。
from random import randint
class Human(object):
# Question is
# Is there a way of getting sword, punch and kick interger values?
attack_moves = {
'fighter': {'sword': randint(1,8),'punch': randint(1,4),
'kick': randint(1,5)},
'Wizard': {'fire ball': randint (1,7),'staff': randint(1,6)},
}
def __init__(self, name,
present_human_hit_points, max_human_hit_points):
self.name = name
self.present_human_hit_points = present_human_hit_points
self.max_human_hit_points = max_human_hit_points
def decrease_health(self, amount):
self.amount = amount
death = False
while not death:
if self.present_human_hit_points > 0:
self.present_human_hit_points -= amount
print self.present_human_hit_points
else:
death = True
class Fighter(Human):
def __init__(self, name,
present_human_hit_points, max_human_hit_points):
Human.__init__(self, name,
present_human_hit_points, max_human_hit_points)
Human.attack_moves = Human.attack_moves.get('fighter')
#def attack_type(self, attack_moves):
# self.attack_moves = Human.attack_moves
# print attack_moves
human = Fighter("bob", 50, 100)
print human.name, human.present_human_hit_points,
print human.max_human_hit_points
print human.attack_moves