I'm trying to code a game, in which the monsters are stored in a database. I'm trying to select a random monster from the database and store the information into variables that I can then use in the rest of the code. I also create the database within my main code. I'm getting the error "NameError: name 'random_monster_type' is not defined" and I'm not really sure why. Any insight would be very helpful.
from monster import Monster
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""CREATE TABLE monster (
number integer,
name text,
type text,
health integer,
defence integer,
attack integer,
crit_chance integer,
level integer,
PRIMARY KEY (number)
)""")
frost_troll = Monster('1', 'troll', 'frost', 90000, 0, 1, 10, 20)
earthy_troll = Monster('2', 'troll', 'earthy', 5000000, 1, 5, 0, 20)
lightning_zard = Monster('3', 'zard', 'lightning', 10000, 0, 10, 5, 1)
ogre_man = Monster('4', 'man', 'ogre', 750000, 25, 50, 0, 10)
chungus_rabbit = Monster('5', 'rabbit', 'chungus', 700, 0, 0, 0, 1)
bdsm_sonic = Monster('6', 'sonic', 'bdsm', 3000, 0, 0, 0, 1)
def insert_mon(mon):
with conn:
c.execute("INSERT INTO monster VALUES (:number, :name, :type, :health, :defence, :attack, :crit_chance, :level)", {'number': mon.number, 'name': mon.name, 'type': mon.type, 'health': mon.health, 'defence': mon.defence, 'attack': mon.attack, 'crit_chance': mon.crit_chance, 'level': mon.level,})
insert_mon(frost_troll)
insert_mon(earthy_troll)
insert_mon(lightning_zard)
insert_mon(ogre_man)
insert_mon(chungus_rabbit)
insert_mon(bdsm_sonic)
c.execute("SELECT Count(*) FROM monster")
temp_rand_half = c.fetchall()
temp_rand_half = (temp_rand_half[0])
temp_rand_half = list(temp_rand_half)
rand_half = temp_rand_half[0]
rand_half = int(rand_half)
def generate_monster():
monnumber = random.randint(1,(rand_half))
c.execute("SELECT * FROM monster WHERE number=:number", {'number': monnumber})
monsterlist = (c.fetchone())
print(monsterlist)
random_monster_name = (monsterlist[1])
random_monster_type = (monsterlist[2])
random_monster_health = (monsterlist[3])
monster_total_health = (monsterlist[3])
monster_defence = (monsterlist[4])
monster_attack = (monsterlist[5])
monster_crit_chance = (monsterlist[6])
monster_level = (monsterlist[7])
generate_monster()
conn.commit()
joined_random_monster = str((random_monster_type) + "_" + (random_monster_name))
themob = pygame.image.load(f"assets\\image\\{joined_random_monster}.png")
print(themob)
conn.commit()
this is my monster python file the main one imports:
class Monster:
"""A sample Monster class"""
def __init__(self, number, name, type, health, defence, attack, crit_chance, level):
self.number = number
self.name = name
self.type = type
self.health = health
self.defence = defence
self.attack = attack
self.crit_chance = crit_chance
self.level = level
@property
def email(self):
return '{}.{} the destroyer'.format(self.name, self.type)
@property
def monstername(self):
return '{} {}'.format(self.name, self.type)
def __repr__(self):
return "Monster('{}', '{}', {})".format(self.name, self.type)
1 Answer 1
You are attempting to reference the variable random_monster_type, which was declared inside the function generate_monster, outside of the function.
To call the variable you have a few options.
Solution 1: Using return
# Solution using return.
def foo():
a = 'bar'
return (100,a)
print(foo()[0]) # 100
print(foo()[1]) # bar
Solution 2: Using global
# Solution using global.
def foo():
global a
a = 'bar'
return 100
print(foo()) # 100
print(a) # 'bar'
Solution 3: Using function attributes
# Solution using function attributes.
def foo():
foo.a = 'bar'
return 100
print(foo()) # 100
print(foo.a) # 'bar'