鱼C论坛

 找回密码
 立即注册
查看: 7157|回复: 25

[技术交流] Python: 每日一题 50

[复制链接]
发表于 2017-7-10 14:53:28 | 显示全部楼层

  1. class Fighter(object):
  2.     def __init__(self, name, health, damage_per_attack):
  3.         self.name = name
  4.         self.health = health
  5.         self.damage_per_attack = damage_per_attack


  6. def declare_winner(f1, f2, first_pick):
  7.     if f1.name == first_pick:
  8.         while True:
  9.             f2.health -= f1.damage_per_attack
  10.             if f2.health < 0:
  11.                 print('{} attack {},{} now has {} health and is dead,{} win'.format(f1.name, f2.name, f2.name, f2.health, f1.name))
  12.                 return f1.name
  13.             else:
  14.                 print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
  15.             f1.health -= f2.damage_per_attack
  16.             if f1.health < 0:
  17.                 print('{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health, f2.name))
  18.                 return f2.name
  19.             else:
  20.                 print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
  21.     else:
  22.         while True:
  23.             f1.health -= f2.damage_per_attack
  24.             if f1.health < 0:
  25.                 print(
  26.                     '{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health,f2.name))
  27.                 return f2.name
  28.             else:
  29.                 print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
  30.             f2.health -= f1.damage_per_attack
  31.             if f2.health < 0:
  32.                 return f1.name
  33.             else:
  34.                 print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
  35. if __name__ == '__main__':
  36.     f1 = Fighter('johny j', 10, 2)
  37.     f2 = Fighter('jin', 15, 3)

  38.     winner_name = declare_winner(f1, f2, 'jin')
复制代码


感觉比较臃肿,想看看大佬怎么写的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-22 08:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表