Python online exam chapter 5 test questions

League of Legends

[Problem description]
League of Legends is a popular multiplayer online competitive game. Now you need your help to implement a hero class (named after Legend).

Each hero should have its own name (name), HP (HP) attack power (attack) These three directly accessible object variables are assigned during initialization.

When the hero is hurt, call the damage calculation function (get _damage, the function should have a parameter indicating the value of the damage received). After the damage is calculated, if the blood volume is higher than 0, the hero name + "keep fighting!" will be output; otherwise, fight Die, output hero name + "die for glory.";

Now because the game time is too long, a mechanism is set up to rewrite the <operator, so that two hero objects can be directly compared (ob1<ob2 compares like this, and returns the Boolean value of the comparison result of the blood volume of the two hero objects.

The simplified game process is: initialize two heroes, and attack each other once, and then use the mechanism mentioned above to determine the winner. Given the process code, only the Legend class needs to be implemented.

[Input form]

Two lines, the corresponding attributes of each hero

[Output form]

The two-line battle process and the final result of the game

[Sample input]

we are 1000 500

timoo 600 700

[Sample export]
garen keep fighting!
Timoo keep fighting!
Garen win!

I really can’t stand it.
In the get_damage function, you must remember that it’s print instead of return.

class Legend:
    def __init__(self,name,HP,attack):
        self.name=name
        self.HP=HP
        self.attack=attack
    def get_damage(self,attack):
        self.HP-=attack
        if self.HP>0:
            print(self.name+' keep fighting')
        else:
            print(self.name+' die for glory')
    def __lt__(self,other):
        return self.HP<other.HP

# 读取红色方和蓝色方两个英雄的信息文件
red_s=input().split()
bule_s=input().split()
# 初始化两个英雄
red=Legend(red_s[0],int(red_s[1]),int(red_s[2]))
bule=Legend(bule_s[0],int(bule_s[1]),int(bule_s[2]))
# 两个英雄各自攻击一次
red.get_damage(bule.attack)
bule.get_damage(red.attack)
# 最后的较量
if red<bule:
    print(bule.name+' win!')
else:
    print(red.name+' win!')

Guess you like

Origin blog.csdn.net/qq_53029299/article/details/115139316