鱼C论坛

 找回密码
 立即注册
查看: 938|回复: 4

[已解决]关于类与对象的属性的问题

[复制链接]
发表于 2021-2-4 14:55:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 woshigoudaner 于 2021-2-4 15:05 编辑

求助各位大神,我在一个外站找到了一个python练习题,要求是创建两个类(Warrior,Knight),以及一个函数(fight),具体要求如下:

In this mission (and in several subsequent ones, related to it) you’ll have a chance "to sit in the developer's chair" and create the logic of a simple game about battles.
Let's start with the simple task - one-on-one duel. You need to create the class Warrior , the instances of which will have 2 parameters - health (equal to 50 points) and attack (equal to 5 points), and 1 property - is_alive, which can be True (if warrior's health is > 0) or False (in the other        case). In addition you have to create the second unit type - Knight, which should be the subclass of the Warrior but have the increased attack - 7. Also you have to create a function fight() , which will initiate the duel between 2 warriors and define the strongest of them. The duel occurs according to the following principle:
Every turn, the first warrior will hit the second and this second will lose his health in the same value as the attack of the first warrior. After that, if he is still alive, the second warrior will do the same to the first one.
The fight ends with the death of one of them. If the first warrior is still alive (and thus the other one is not anymore), the function should return True , False otherwise.
Input: The warriors.
Output: The result of the duel (True or False).
Precondition:
2 types of units
All given fights have an end (for all missions).

以下是我写的代码以及网站给定的测试代码是否正确的方法(第二部分为网站给定,不可修改)
  1. class Warrior:
  2.     def __init__(self,health=50,attack=5):
  3.         self.health = health
  4.         self.attack = attack
  5.         
  6.     def is_alive(self):
  7.         if self.health <= 0:
  8.             return False
  9.         else:
  10.             return True

  11. class Knight(Warrior):
  12.     def __init__(self, health=50, attack=7):
  13.         self.health = health
  14.         self.attack = attack

  15. def fight(unit_1, unit_2):
  16.     while unit_1.health > 0 and unit_2.health > 0:
  17.         unit_2.health -= unit_1.attack
  18.         if unit_2.health <= 0:
  19.             return True
  20.             break
  21.         else:
  22.             unit_1.health -= unit_2.attack
  23.             if unit_1.health <= 0:
  24.                 return False
  25.                 break
  26.             else:
  27.                 continue
复制代码

  1. <font color="#ff0000">if __name__ == '__main__':
  2.     #These "asserts" using only for self-checking and not necessary for auto-testing

  3.     chuck = Warrior()
  4.     bruce = Warrior()
  5.     carl = Knight()
  6.     dave = Warrior()
  7.     mark = Warrior()

  8.     assert fight(chuck, bruce) == True
  9.     assert fight(dave, carl) == False
  10.     assert chuck.is_alive == True
  11.     assert bruce.is_alive == False
  12.     assert carl.is_alive == True
  13.     assert dave.is_alive == False
  14.     assert fight(carl, mark) == False
  15.     assert carl.is_alive == False

  16.     print("Coding complete? Let's try tests!")</font>
复制代码


同时附上运行后产生错误的结果(同样,为网站自动运行结果,红色部分不可修改):

bob = Warrior()
mars = Warrior()
fight(bob, mars)
bob.is_alive
TypeError: <class 'dict'> is wrong data type


并附上原网站网址:https://py.checkio.org/mission/the-warriors/solve/
求助各位,如果能解决将万分感谢!
最佳答案
2021-2-4 19:39:48


帮你改了下代码,先试试测试看看能不能 Pass:

  1. class Warrior:
  2.     def __init__(self, health=50, attack=5):
  3.         self.health = health
  4.         self.attack = attack
  5.         self.is_alive = True

  6. class Knight(Warrior):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.attack = 7

  10. def fight(unit_1, unit_2):
  11.     while unit_1.health > 0 and unit_2.health > 0:
  12.         unit_2.health -= unit_1.attack
  13.         if unit_2.health > 0:
  14.             unit_1.health -= unit_2.attack
  15.             if unit_1.health > 0:
  16.                 continue
  17.             unit_1.is_alive = False
  18.             return False
  19.         unit_2.is_alive = False
  20.         return True
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-4 14:57:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 17:31:07 | 显示全部楼层
is_alive应该是一个属性,而不是函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 19:39:48 | 显示全部楼层    本楼为最佳答案   


帮你改了下代码,先试试测试看看能不能 Pass:

  1. class Warrior:
  2.     def __init__(self, health=50, attack=5):
  3.         self.health = health
  4.         self.attack = attack
  5.         self.is_alive = True

  6. class Knight(Warrior):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.attack = 7

  10. def fight(unit_1, unit_2):
  11.     while unit_1.health > 0 and unit_2.health > 0:
  12.         unit_2.health -= unit_1.attack
  13.         if unit_2.health > 0:
  14.             unit_1.health -= unit_2.attack
  15.             if unit_1.health > 0:
  16.                 continue
  17.             unit_1.is_alive = False
  18.             return False
  19.         unit_2.is_alive = False
  20.         return True
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-4 20:55:49 | 显示全部楼层
Twilight6 发表于 2021-2-4 19:39
帮你改了下代码,先试试测试看看能不能 Pass:

通过了,十分感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 10:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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