从其他功能和模块调用字典

时间:2020-06-27 18:33:12

标签: python

很抱歉提出一个新手问题,但是我正在学习函数,模块和字典,并且在组合这三个概念时遇到了麻烦。基本上我的问题是,有没有办法在另一个模块的函数中创建字典并像在当前模块的函数中一样使用它?

我这样做的失败尝试是在我的文字RPG中:

对于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

monsters.py模块超过700行,但我将尝试发布所有相关代码:

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

当程序首次尝试加载战斗循环时,此代码崩溃:

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

同样,我只是在学习这些概念,所以我什至不确定我要做什么。在添加功能和模块之前,代码已工作。任何建议表示赞赏。谢谢!

此外,如果我将种子怪物列表粘贴到main.py中,则此代码也有效,但它显示“无显示!”。主程序似乎无法从其他模块的功能中提取字典。

2 个答案:

答案 0 :(得分:1)

您只是忘记捕获monsters.monster_list的返回值:

monster = monsters.monster_list(hero)
print(f"A {monster['name']} appears!")

您似乎很困惑,因为您最初将monster作为全局变量,每个循环都对其进行了修改(这是非常糟糕的设计-一个新手错误),并且您没有完全切换到返回它。同样,monster['name'] = slime['name']是同一不良设计的一部分。相反,让monsters.monster_list只需返回current_monster

if hero['tier'] == 1:
    return random.choice(tier1_monsters)

答案 1 :(得分:0)

我假设您在Monsters模块中有一个名为Monster的变量。 import monster使您能够访问该库,但通常不会将所有这些函数和变量都带入本地名称空间。要访问此类变量,您需要将其引用为:

monsters.monster[name]

或者,您可以使用

将其带入本地名称空间
from monsters import monster