鱼C论坛

 找回密码
 立即注册
查看: 1053|回复: 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).

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

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

def fight(unit_1, unit_2):
    while unit_1.health > 0 and unit_2.health > 0:
        unit_2.health -= unit_1.attack
        if unit_2.health <= 0:
            return True
            break
        else:
            unit_1.health -= unit_2.attack
            if unit_1.health <= 0:
                return False
                break
            else:
                continue
<font color="#ff0000">if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing

    chuck = Warrior()
    bruce = Warrior()
    carl = Knight()
    dave = Warrior()
    mark = Warrior()

    assert fight(chuck, bruce) == True
    assert fight(dave, carl) == False
    assert chuck.is_alive == True
    assert bruce.is_alive == False
    assert carl.is_alive == True
    assert dave.is_alive == False
    assert fight(carl, mark) == False
    assert carl.is_alive == False

    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:
class Warrior:
    def __init__(self, health=50, attack=5):
        self.health = health
        self.attack = attack
        self.is_alive = True

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

def fight(unit_1, unit_2):
    while unit_1.health > 0 and unit_2.health > 0:
        unit_2.health -= unit_1.attack
        if unit_2.health > 0:
            unit_1.health -= unit_2.attack
            if unit_1.health > 0:
                continue
            unit_1.is_alive = False
            return False
        unit_2.is_alive = False
        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:
class Warrior:
    def __init__(self, health=50, attack=5):
        self.health = health
        self.attack = attack
        self.is_alive = True

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

def fight(unit_1, unit_2):
    while unit_1.health > 0 and unit_2.health > 0:
        unit_2.health -= unit_1.attack
        if unit_2.health > 0:
            unit_1.health -= unit_2.attack
            if unit_1.health > 0:
                continue
            unit_1.is_alive = False
            return False
        unit_2.is_alive = False
        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, 2025-1-16 15:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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