简体   繁体   中英

how do i get monster to exist in def main() Battle(monster)

I have a list of monsters it is choosing from.

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

it is sending me this type error: attack() missing 1 required positional argument: 'monster' at me and i cant figure what that means

Your attack() function takes a single positional argument monster (you stipulated this requirement, when you defined your function):

def attack(monster):
    ...

When you call it like this:

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

you are failing to pass that argument, causing an error. Instead, you need to pass the monster object to the function when you call it:

attack(monster)

From looking at the rest of your code, it looks like you could do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM