鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: ooxx7788

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

[复制链接]
发表于 2019-9-10 13:34:01 | 显示全部楼层
  1. class Fighter:
  2.           def __init__(self,name,health,attack):
  3.                     self.name = name
  4.                     self.health = health
  5.                     self.damage_per_attack = attack
  6.           def fight(self,Fb):
  7.                     print(self.name+' attacks '+Fb.name)
  8.                     Fb.health -= self.damage_per_attack
  9.                     print(Fb.name+' now has %d health'%(Fb.health))
  10. if __name__=='__main__':
  11.           def declare_winner(fa,fb,name):
  12.                     if fa.name == name:
  13.                               fa.fight(fb)
  14.                               while fa.health > 0 and fb.health > 0:
  15.                                         fb.fight(fa)
  16.                                         fa.fight(fb)
  17.                     else:
  18.                               fb.fight(fa)
  19.                               while fa.health > 0 and fb.health > 0:
  20.                                         fa.fight(fb)
  21.                                         fb.fight(fa)
  22.                     name = fa.name if fa.health > 0 else fb.name
  23.                     print(name+' wins')
  24.           declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 14:36:37 | 显示全部楼层
  1. class Fighter():
  2.     def __init__(self, name, health, dpa):
  3.         self.name = name
  4.         self.health = health
  5.         self.dpa = dpa

  6.     def attack(self, other):
  7.         other.health -= self.dpa
  8.         if other.health > 0:
  9.             print('{a} attacks {b};{b}  now has {c} health.'.format(a=self.name, b=other.name, c=other.health))
  10.         else:
  11.             print('{a} attacks {b};{b}  now has {c} health and is dead.'.format(a=self.name, b=other.name, c=other.health))

  12. # 递归实现互相攻击
  13. def declare_winner(fighter_a, fighter_b, name):
  14.     if fighter_a.health < 0:
  15.         return print('{} wins'.format(fighter_b.name))
  16.     elif fighter_b.health < 0:
  17.         return print('{} wins'.format(fighter_a.name))

  18.     if fighter_a.name == name:
  19.         fighter_a.attack(fighter_b)
  20.         declare_winner(fighter_a, fighter_b, fighter_b.name)
  21.     else:
  22.         fighter_b.attack(fighter_a)
  23.         declare_winner(fighter_a, fighter_b, fighter_a.name)


  24. declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Harry")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-2 22:28:43 From FishC Mobile | 显示全部楼层
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-20 09:51:36 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-17 20:49:47 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-20 21:26:43 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 16:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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