Reputation: 33
Sorry for asking a newbie question, but I'm learning about functions, modules, and dictionaries, and having trouble combining the three concepts. Basically my question is, is there a way to create a dictionary in another module's function and use it as if it were in the current module's function?
My failed attempt at doing this is in my text RPG:
For the main.py:
import monsters # At top of program
monsters.monster_list # In the middle of the program
# Command loop during combat
def combat_loop():
in_town = False
monster_appear = True
combat_active = True
while monster_appear == True:
# Pull a random monster from the appropriate level pool.
monsters.monster_list(hero)
print(f"A {monster['name']} appears!")
print(f"You have {hero['current_health']} / {hero['max_health']} health remaining.")
monster_appear = False
The monsters.py module is over 700 lines but I'll try to post all the relevant code:
def monster_list(hero):
# Monster List
# Seed stats
monster = {
'name': "None",
'health': 0,
# Monster strength is used in figuring out how much damage will be done to you.
'strength' : 0,
# Monster agility is used in figuring out how much damage will be reduced for the enemy.
'agility' : 0,
'gold': 0,
'exp': 0,
}
# Slime stats
slime = {
'name': "Slime",
'health': 3,
'strength' : 5,
'agility' : 3,
'gold': 1,
'exp': 1
# Slime has a 1/64 chance to Dodge
}
# Monster sets per level (edited for length).
monsters = ['slime', etc]
# The following makes a list slice out of monsters from position 0
# up to but not including 2 and so on.
tier1_monsters = monsters[0:2]
if hero['tier'] == 1:
current_monster = random.choice(tier1_monsters)
#Omitted the rest for length
# Monster stats list - attached base slime stats to the current monster in combat
if current_monster == 'slime':
monster['name'] = slime['name']
monster['health'] = slime['health']
monster['strength'] = slime['strength']
monster['agility'] = slime['agility']
monster['gold'] = slime['gold']
monster['exp'] = slime['exp']
# Omitted for length
return monster
This code crashes when the program tries to load the combat loop for the first time:
Traceback (most recent call last):
File "my_rpg_modules.py", line 781, in <module>
combat_loop()
File "my_rpg_modules.py", line 717, in combat_loop
print(f"A {monster['name']} appears!")
NameError: name 'monster' is not defined
Again, I'm only just learning about these concepts so I'm not even sure what I'm trying to do is possible. The code worked before I added functions and modules. Any advice is appreciated. Thanks!
Also, this code works if I paste the seed monster list into the main.py, but it says "A None appears!". The main program can't seem to pull the dictionary from the other module's function.
Upvotes: 0
Views: 48
Reputation: 33087
You just forgot to capture the return value of monsters.monster_list
:
monster = monsters.monster_list(hero)
print(f"A {monster['name']} appears!")
It seems like you're confused because you originally had monster
as a global that was modified every loop (which is very bad design - a newbie mistake), and you didn't fully switch to returning it. On that same note, monster['name'] = slime['name']
is part of the same bad design. Instead, have monsters.monster_list
simply return the current_monster
:
if hero['tier'] == 1:
return random.choice(tier1_monsters)
Upvotes: 1
Reputation: 4539
I assume you have a variable in your monsters module named monster. import monster
gives you the ability to access the library, but it does not typically bring all those functions and variables into the local namespace. To access such variable, you'll need to reference it as:
monsters.monster[name]
Alternatively, you can bring it into the local namespace with
from monsters import monster
Upvotes: 0