尝试从数据库中选择信息不起作用



我正在尝试编写一个游戏代码,其中怪物存储在数据库中。我试图从数据库中选择一个随机的怪物,并将信息存储到变量中,然后我可以在其余的代码中使用这些变量。我还在我的主代码中创建数据库。我得到了错误";名称错误:名称"random_moster_type"未定义"我真的不知道为什么。任何见解都会很有帮助。

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()

这是我的怪物python文件,主要导入一个:

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) 

您正试图引用变量random_monster_type,该变量是在函数generate_monster内部、函数外部声明的。

要调用变量,您有几个选项。

解决方案1:使用返回

# Solution using return.  

def foo(): 
a = 'bar'
return (100,a) 

print(foo()[0]) # 100
print(foo()[1]) # bar 

解决方案2:使用全局

# Solution using global.
def foo(): 
global a 
a = 'bar'
return 100 

print(foo())  # 100
print(a)      # 'bar'

解决方案3:使用功能属性

# Solution using function attributes.
def foo():  
foo.a = 'bar'
return 100

print(foo()) # 100
print(foo.a) # 'bar'

最新更新