Following is the code for the game which I have written:
from random import randint
class Dice:
def die(num):
die=randint(1,num)
return die
class Character:
def __init__(self,name,hp,damage):
self.name=name
self.hp=hp
self.damage=damage
class Fighter(Character):
def __init__(self):
super().__init__(name=input("what is your character's name?"),
hp=20,
damage=12)
prof= "fighter"
class Assasin(Character):
def __init__(self):
super().__init__(name=input("what is your character's name?"),
hp=15,
damage=14)
prof= "assasin"
class Goblin(Character):
def __init__(self):
super().__init__(name="goblin",
hp=15,
damage=3)
class Ogre(Character):
def __init__(self):
super().__init__(name="ogre",
hp=25,
damage=6)
def profession():
print("What is your class?",'\n',
"Press f for fighter" , '\n',
"Press a for assasin")
pclass=input("Enter your choice>>>")
if(pclass=="f"):
prof=Fighter()
elif(pclass=="a"):
prof=Assasin()
else:
prof=Fighter()
return prof
def ranmob():
mob=Ogre() if Dice.die(2)<2 else Goblin()
return mob
def playerAttack():
roll=Dice.die(10)
print("You hit")
if (hero.prof=="fighter"):
if(roll<8):
print("them for 12 damage")
mob.hp-=12
print("The",mob.name,"has",mob.hp,"hp left")
else:
print("You missed your attack")
elif(hero.prof=="assasin"):
if(roll<8):
print("them for 14 damage")
mob.hp-=14
print("The",mob.name,"has",mob.hp,"hp left")
else:
print("You missed your attack")
def monsterAttack():
roll=Dice.die(10)
print("The monster attacks")
if(mob.name=="Ogre"):
if(roll<8):
print("6 damage taken")
hero.hp-=6
else:
print("The attack misses")
elif(mob.name=="Goblin"):
if(roll<8):
print("3 damage taken")
hero.hp-=3
else:
print("The attack misses")
def commands():
if hero.prof=="fighter":
print("press f to fight\n","press e to pass")
command=input(">>>>>")
if(command=="f"):
playerAttack()
elif command=="e":
pass
elif hero.prof=="assasin":
print("press f to fight\n","press e to pass")
command=input(">>>>>")
if(command=="f"):
playerAttack()
elif command=="e":
pass
mob=ranmob()
hero=profession()
print("name hp"'\n',hero.name,hero.hp)
while True:
if mob.hp<=0:
print('The',mob.name,'is dead')
mob=ranmob()
if hero.hp<=0:
print(hero.name,'died!')
hero=profession()
print("name hp",'\n',hero.name,hero.hp)
print("You see",mob.name,",",mob.name,"has",mob.hp,"hp")
if hero.hp>0:
commands()
if mob.hp>0:
monsterAttack()
Output throws the error-
Traceback (most recent call last):
File "C:\Users\Hi\Desktop\python programs\nvm\MYFIRSTGAME.py", line 129, in <module>
commands()
File "C:\Users\Hi\Desktop\python programs\nvm\MYFIRSTGAME.py", line 98, in commands
if hero.prof=="fighter":
AttributeError: 'Fighter' object has no attribute 'prof'
Any form of help or suggestion would be appreciated :-)
What I have tried:
Tried tinkering with the prof in the Fighter and assasin clas,but didnt work out