I have a code that I've been messing around with and the monster attack (monster.ma) isn't randomising after the first time it's called upon (elif statement). I've tried to make it a tuple since they are immutable but it doesn't randomise after the first time. I was told to make a class and that's what I have done but it still doesnt work right. My attack variable (att) works right, but monsters' attack gets stuck. I know I can code the monster's attack into the elif statement, but I want to be able to just call upon it when needed.

import random

class monster():
    ma=random.randint(5, 20)
    xp=random.randint(15, 30)
while a==1:
    if monster.hp<=0:
        print"you have killed the monster"
        print("you get, "+str(monster.xp)+" exp from monster"
              "you now have, "+str(exp)+" exp")
    elif monster.hp>=0:
        print"swing your sword"
        att=random.randint(5, 15)
        print"you do, "+str(att)+" damage to monster"
        print"monster does, "+str(monster.ma)+" damage to you" #<----------------here

It's only randomized the first time because you're only calling the random function one time (when you create the monster object). When you have ma=random.randmint(5, 20) in your class then you are creating that variable with a random amount at that time, but you are not altering it. In order to get a random value every time you need to reset the value every time you perform an attack. Notice how you do this when you set att; you need to do it the same way with monster.ma.

If you want to be able to generate a random attack, you need to create a method to do it. Something like this in your monster class:

def get_attack():
    return random.randomint(5, 20)

Should work whenever you call monster.get_attack(). Note however that it's generally a bad idea to use hard coded "magic numbers" like this. It would be a good idea to define a monsters attack stats as members of the monster class. It seems like you're making monster a class because you read somewhere that it should be a class and not because you're going for an object oriented structure. Read up on object oriented programming

  • \$\begingroup\$ Why not also encourage him to use minDmg and maxDmg constants instead of hardcoded 5's and 20's \$\endgroup\$
    – bobobobo
    Sep 17 '13 at 2:11
  • 1
    \$\begingroup\$ Actually I did consider that but opted for the simplest answer since outlining how to make a properly constructed monster class seemed out of the scope of answering why random damage doesn't work right. Perhaps some suggestions would be good anyway, though. \$\endgroup\$
    – ssb
    Sep 17 '13 at 2:32

I'm very rusty with python, but usually what you want to do is create new monsters.

For example,

class monster(): def init(self): self.ma = random.randint(5,20)

or something. That way, you can create new monsters with m = monster() and then use their attack. I'm not sure this is perfectly correct, but it should help.


Not the answer you're looking for? Browse other questions tagged or ask your own question.