PrQ PrQ - 2 months ago 4x
Python Question

Using Random to call a dictionary from a different class

I'm making a small game and I'm trying to have the EncounterM class get a random Monster and Dmg from the monster class dictionary. (New at Python)

class Monster:

monster_health = {'goblin': 15, 'giant': 50}
monster_damage = {'goblin': 3, 'giant': 1}

class EncounterM(Monster):
import random

# I tried using random.choice(monster_health.keys())


I'll ignore the not-working-at-all object constructions to focus on your question. Read more about object, instances, classes and post another question. (Edit: I couldn't resist to change it a little to make it a little more object-like, but I won't go further)

With Python 3, keys() do not return a list anymore. You have to cast to list explicitly for it to work

import random

class Monster:
   health = {'goblin': 15, 'giant': 50}
   damage = {'goblin': 3, 'giant': 1}



(or goblin)