Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have successfully compiled this code in python 3.6,but when I try to do the same it throws an error-

Code-

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)
        self.prof= "fighter"
        
class Assasin(Character):
    def __init__(self):
        super().__init__(name=input("what is your character's name?"),
                       hp=15,
                       damage=14)
        self.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():
    roll=Dice.die(10)
    if roll<8:
        mob=Ogre()
    else:
        mob=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)
    if (mob.name=="ogre"):
        print("THE OGRE SHRIEKS and attacks")
        if(roll<8):
            print("6 damage taken")
            hero.hp-=6
            print("You now have",hero.hp,"hp left")
        else:
            print("The attack misses")

    elif (mob.name=="goblin"):
        print("The goblin prepares his knife and jumps")
        if(roll<8):
            print("3 damage taken")
            hero.hp-=3
            print("You now have",hero.hp,"hp left")
        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()

Error-

Traceback (most recent call last):
  File "C:\Users\Hi\Desktop\python programs\2.7trail.py", line 120, in <module>
    mob=ranmob()
  File "C:\Users\Hi\Desktop\python programs\2.7trail.py", line 59, in ranmob
    roll=Dice.die(10)
TypeError: unbound method die() must be called with Dice instance as first argument (got int instance instead)


What I have tried:

Not sure about the error.(......................................)
Posted
Updated 1-Feb-18 5:39am
v2

1 solution

Add a staticmethod decorator:
Python
class Dice:
    @staticmethod
    def die(num):
        die=randint(1,num)
        return die
The reason for the error: Python 2 requires the first argument to be an instance of the class itself; in Python 3 it can be anything.

[Edit]

Changing that alone won't make your code run perfectly in Python 2, though. Python 3's input takes input and returns a string with that input, but the Python 2 equivalent of that is raw_input. Python 2 has an input function too, but that doesn't just take input, it also evaluates it (so Python 2's input would be eval(input()) in Python 3). See this Stack Overflow question for backwards-compatible input calls in Python[^].
 
Share this answer
 
v3
Comments
Sushmit Chakraborty 1-Feb-18 12:03pm    
Thankyou :-)

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