In: Computer Science
Python
I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name
import random
class Hero:
def __init__(self,ammo,health):
self.ammo=ammo
self.health=health
def blast(self):
print("The Hero blasts an Alien!")
if self.ammo>0:
self.ammo-=1
return True
else:
print("Oh no! Hero is out of ammo.")
return False
def damage(self):
self.health-=1
if self.health==0:
print("Hero is out of health. Alien wins.")
else:
print("Hero took damage")
class Alien:
def __init__(self,ammo,health):
self.ammo=ammo
self.health=health
def blast(self):
print("Alien is blasting")
if self.ammo>0:
self.ammo-=1
return True
else:
print("Oh no! Alien is out of ammo.")
return False
def damage(self):
self.health-=1
if self.health==0:
print("Alien is out of health. Hero wins.")
else:
print("Alien took damage")
def main():
h=Hero(10,10)
a=Alien(10,10)
while h.health>0 and a.health>0:
move=random.randint(0,1)
if move==0:
if h.blast():
a.damage()
else:
if a.blast():
h.damage()
main()
add name in Alien and Hero classes as in below.. give your name in the main method.. you can see it from the below screens.. make sure that you follow proper indentations...
code:
import random
class Hero:
def __init__(self,ammo,health,name):
self.ammo=ammo
self.health=health
self.name = name
def blast(self):
print(self.name+" blasts an Alien!")
if self.ammo>0:
self.ammo-=1
return True
else:
print("Oh no! "+self.name+" is out of ammo.")
return False
def damage(self):
self.health-=1
if self.health==0:
print(self.name+" is out of health. Alien wins.")
else:
print(self.name+" took damage")
class Alien:
def __init__(self,ammo,health,name):
self.ammo=ammo
self.health=health
self.name = name
def blast(self):
print(self.name+" is blasting")
if self.ammo>0:
self.ammo-=1
return True
else:
print("Oh no! "+self.name+" is out of ammo.")
return False
def damage(self):
self.health-=1
if self.health==0:
print(self.name+" is out of health. Hero wins.")
else:
print(self.name+" took damage")
def main():
h=Hero(10,10,"Iron Man")
a=Alien(10,10,"Thanos")
while h.health>0 and a.health>0:
move=random.randint(0,1)
if move==0:
if h.blast():
a.damage()
else:
if a.blast():
h.damage()
main()