马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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/
求助各位,如果能解决将万分感谢!
帮你改了下代码,先试试测试看看能不能 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
|