Using function as argument results in "takedamage() takes exactly 3 arguments (2 given)" error

StackOverflow https://stackoverflow.com/questions/23617558

  •  21-07-2023
  •  | 
  •  

Question

First of all, beware, first time asker and very new programmer here, the following question and/or code may contain extreme noobness...

I have a function takedamage(self, accuracy, dmg) within the class hostilemelee. If I give it the arguments "manually" IE:

testnpc = hostilemelee()
testnpc.takedamge(5, 20) 

the function runs fine. But if I use the function ironman.meleeatack(5) which returns 2 numbers as the arguments, I get the "Needs exactly 3 arguments" error. The ironman.meleeatack function is:

def meleeattack(self, aggression):   
    accroll = randint(self.maccuracy,100)  
    powerroll = randint(self.strength,100)
    meleedmg = (powerroll * (1 + aggression / 10)) 
    if randint(0, 15) < aggression:
        dmg = 0
    else:
        dmg = meleedmg + self.bdamage
    return accroll, dmg    

And the testnpc.takedamage function is:

    def takemdamage(self, accuracy, dmg):
    if dmg == 0:
        print "Ironman's attack misses!" 
        self.dmgmitigated = 0
    elif accuracy < self.accreq:
        print "%s evades the attack!" % self.name 
        self.dmgmitigated = "All"
    else:
        self.health = self.health - (.25 * dmg) - .75 * (dmg - \
        (self.armor / 100) * dmg)
        self.dmgmitigated = .75 * (self.armor / 100 * dmg) 
Was it helpful?

Solution

You need to unpack the result of meleeattack. Try testnpc.takedamge(*ironman.meleeatack(5)) instead. Without the asterisk, you are passing a tuple as a single argument, with the asterisk, the interpreter unpacks the values in the tuple in to 2 arguments before passing them to the takedamage method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top