Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Following is the code for the game which I have written:

Python
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"

##NOW FOR ENEMIES

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)

##ENEMIES

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
Posted
Updated 1-Feb-18 3:21am
v2
Comments
Richard MacCutchan 1-Feb-18 10:15am    
I gave you the answer in the previous question. Please do not repost.
Sushmit Chakraborty 1-Feb-18 10:55am    
Sorry,but I went back to coding after the 1st solution,didnt see your response by the time I posted this question.

1 solution

Quote:
prof= "fighter"
You probably meant
self.prof = "fighter"
 
Share this answer
 
Comments
Sushmit Chakraborty 1-Feb-18 9:29am    
Thankyou yet again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900