So I started programming a month ago. I took up python as my learning language and thought of making a rpg game(text-based) from internet sources. Here is the code-
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)
name=input("what is your character's name?")
prof="assasin"
class Goblin(Character):
def _init_(self):
super._init_(name="Goblin",
hp=15,
damage=3)
hp=15
name="Goblin"
damage=3
class Ogre(Character):
def _init_(self):
super._init_(name="Ogre",
hp=25,
damage=6)
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():
if Dice.die(2)<2:
mob=Ogre()
else:
mob=Goblin()
return mob
def playerAttack():
print("You hit")
if (hero.prof=="fighter"):
if(Dice.die(8)<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(Dice.die(8)<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():
print("The monster attacks")
if(mob.name=="Ogre"):
if(Dice.die(8)<8):
print("6 damage taken")
hero.hp-=6
else:
print("The attack misses")
elif(mob.name=="Goblin"):
if(Dice.die(8)<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
mob=ranmob()
hero=profession()
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()
But when I try to run it,it shows the initial selection of characters after which it throws an error-
Traceback (most recent call last):
File "C:/Users/Hi/Desktop/python programs/nvm/MYFIRSTGAME.py", line 117, in <module>
if mob.hp<=0:
AttributeError: 'Goblin' object has no attribute 'hp'
Can anyone help me out here? Thanks in advance :-)
What I have tried:
Not sure what is causing the problem