我有一个可供选择的怪物清单。

    def monsters():
    whatMonster = random.randint(0, 25)
    monster = pickMonster[whatMonster]
    print(monster.Name)
    print("life =", monster.life)
    print("EXP =", monster.exp)
    return monster

def Battle(monster):

    def attack(monster):
        print(monster.Name)
        print("life =", monster.life)
        print("EXP =", monster.exp)
        print()
        print("level", player.lvl)
        print("HP =", player.life)
        print("Ammor =", player.Def)
        pExp = 0
        pExp = monster.exp + pExp

    action = input(" will you [A]ttack or [R]un   ")
    if action == "A":
        print("you attack")
        attack(monster)
    if action == "R":
        print("You runaway")
    return attack
    return monster

def main():
        loop = True
        header()
        print()
        while loop == True:
            game_loop()
            print()
            monsters()
            print()
            player()
            Battle(monster)
            print()


它正在向我发送此类型错误:Attack()缺少1个必需的位置参数:'monster'在我身上,我不知道这意味着什么

最佳答案

您的attack()函数采用单个位置参数monster(在定义函数时,您规定了此要求):

def attack(monster):
    ...


当您这样称呼它时:

if action == "A":
    print("you attack")
    attack()


您无法传递该参数,从而导致错误。相反,您需要在调用monster对象时将其传递给该函数:

attack(monster)


通过查看其余的代码,您似乎可以执行以下操作:

random_monster = monsters()
...
attack(random_monster)

关于python - 我如何让怪物存在于def main()Battle(monster)中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58629273/

10-13 02:39