会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132462个问题
Python 全系列/第一阶段:Python入门/编程基本概念 2941楼
Python 全系列/第一阶段:Python入门/函数和内存分析 2942楼
Python 全系列/第一阶段:Python入门/控制语句 2943楼
Python 全系列/第一阶段:Python入门/控制语句 2945楼

老师,我做练习的时候,遇到问题,我明明写了两个参数但是它提示我还少一个参数

# 定义战士类
class Warrior:
    def __init__(self):
        self.health = 50
        self.attack = 5
        self.is_alive = True


# 定义骑士类
class Knight(Warrior):
    def __init__(self):
        super().__init__()
        self.attack = 7


# 定义军队
class Army:
    def __init__(self):
        self.arm = []

    def add_units(self, soldier, number):
        for i in range(number):
            new_unit = soldier
            self.arm.append(new_unit)
        return self.arm


# 定义战斗类
class Battle:
    def fight(self, armclass1, armclass2):
        arm1 = armclass1.arm
        arm2 = armclass2.arm
        while len(arm1) > 0 and len(arm2) > 0:
            if fight1(arm1[0], arm2[0]) == True:
                arm2.remove(arm2[0])
            else:
                arm1.remove(arm1[0])
        if len(arm1) > 0:
            return True
        else:
            return False


def fight1(unit_1, unit_2):
    while unit_1.is_alive and unit_2.is_alive:
        unit_2.health -= unit_1.attack
        if unit_2.health <= 0:
            unit_2.is_alive = False
            break
        unit_1.health -= unit_2.attack
        if unit_1.health <= 0:
            unit_1.is_alive = False
            break
    return unit_1.is_alive


if __name__ == '__main__':
    # battle tests
    my_army = Army()
    my_army.add_units(Knight, 3)

    enemy_army = Army()
    enemy_army.add_units(Warrior, 3)

    Battle.fight(my_army, enemy_army)

    army_3 = Army()
    army_3.add_units(Warrior, 20)
    army_3.add_units(Knight, 5)

    army_4 = Army()
    army_4.add_units(Warrior, 30)

    battle = Battle()

屏幕截图 2021-02-20 140158.png

Python 全系列/第一阶段:Python入门/面向对象 2947楼
Python 全系列/第一阶段:Python入门/序列 2948楼
Python 全系列/第一阶段:Python入门/函数和内存分析 2949楼
Python 全系列/第一阶段:Python入门/控制语句 2950楼
Python 全系列/第一阶段:Python入门/编程基本概念 2952楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637