|
发表于 2017-7-10 14:53:28
|
显示全部楼层
- class Fighter(object):
- def __init__(self, name, health, damage_per_attack):
- self.name = name
- self.health = health
- self.damage_per_attack = damage_per_attack
- def declare_winner(f1, f2, first_pick):
- if f1.name == first_pick:
- while True:
- f2.health -= f1.damage_per_attack
- if f2.health < 0:
- print('{} attack {},{} now has {} health and is dead,{} win'.format(f1.name, f2.name, f2.name, f2.health, f1.name))
- return f1.name
- else:
- print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
- f1.health -= f2.damage_per_attack
- if f1.health < 0:
- print('{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health, f2.name))
- return f2.name
- else:
- print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
- else:
- while True:
- f1.health -= f2.damage_per_attack
- if f1.health < 0:
- print(
- '{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health,f2.name))
- return f2.name
- else:
- print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
- f2.health -= f1.damage_per_attack
- if f2.health < 0:
- return f1.name
- else:
- print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
- if __name__ == '__main__':
- f1 = Fighter('johny j', 10, 2)
- f2 = Fighter('jin', 15, 3)
- winner_name = declare_winner(f1, f2, 'jin')
复制代码
感觉比较臃肿,想看看大佬怎么写的 |
|