NameError: 'enemy' is not defined

问题描述 投票:0回答:0
#----FIGHTER CLASS----
class Fighter(Animal):
    def __init__(self, name, species, level):
        self.name = name
        self.species = species
        self.level = level
        self.ferocity = 0
        self.toughness = 0
        self.attack_damage = 0.0
        self.total_health = 0
        self.current_health = self.total_health
    
    def __str__(self):
        return self.name
    
    def fighter_age(self):
       self.get_age()
       self.age = self.age / 2
       print(f"{int(round(self.age))} years old ")
       
    def update_health(self):
        self.total_health += self.level
    
    def get_ferocity(self):
        self.ferocity = random.randint(1,100)
        print(f"{self.ferocity}% ferocious")

    def get_level(self):
        self.level = min(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100))
        print(f"lvl {self.level}")
        
    def get_toughness(self):
        self.toughness = random.randint(1, 100)
        print(f"{self.toughness}% toughness")

    def get_species(self):
        species_choice = random.randint(0, len(fighters_list)-1)
        self.species = fighters_list[species_choice]
        print(self.species)
        
    def do_attack(self):
        self.attack_damage = random.randint(10, 100)
        print(self.attack_damage)


    def be_attacked(self, enemy_attack_damage):
        self.current_health -= enemy_attack_damage
        if self.current_health > 0:
            print(f"{self.current_health}/{self.total_health} health")
        elif self.current_health <= 0:
            print("Dead")
    

    def create_stats(self):
        print(f"---{self.name}---")
        print(self.species)
        print(f"lvl {self.level}")
        self.fighter_age()
        self.get_total_health()
        self.current_health = self.total_health
        print(f"{self.total_health}/{self.total_health} health")
        self.get_happiness()
        self.get_ferocity()
        self.get_toughness()
         
    def check_stats(self):
        print("")
        print(f"---{self.name}---")
        print(f"{self.species}")
        print(f"lvl {self.level}")
        print(f"{int(self.age)} years old")
        print(f"{self.current_health}/{self.total_health} health")
        print(f"{self.happiness}% happy")
        print(f"{self.ferocity}% ferocious")
        print(f"{self.toughness}% toughness")

    def level_up(self):
        self.level += 1
        self.update_health()

#----ENEMY FIGHTER CLASS----

class Enemy_Fighter(Animal):
    def __init__(self):
        self.species = ''
        self.level = 0
        self.attack_damage = 0.0
        self.total_health = 0
        self.current_health = self.total_health

    def get_species(self):
        species_choice = random.randint(0, len(fighters_list)-1)
        self.species = fighters_list[species_choice]
        print(self.species)

    def get_level(self):
        self.level = min(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100))
        print(f"lvl {self.level}")

    def create_stats(self):
        self.get_species()
        self.get_level()
        self.get_total_health()
        self.current_health = self.total_health
        print(f"{self.total_health}/{self.total_health} health")

    def get_attack(self):
        self.attack_damage = random.randint(10, 100)
        print(self.attack_damage)

    def be_attacked(self, enemy_attack_damage):
        self.current_health -= enemy_attack_damage
        if self.current_health > 0:
            print(f"{self.current_health}/{self.total_health} health")
        elif self.current_health <= 0:
            print("Dead")

**#----FIGHTER CREATION/REPLACEMENT----
def create_fighter(name: str) -> Fighter:
    creature = Fighter(name, enemy.species, enemy.level)
    creature.create_stats()
    print(" \n \n \n")
    return creature**

def replace_fighter(new_name, replaced: str) -> Fighter:

    fighters.pop(replaced)
    fighters[new_name] = create_fighter(new_name)



fighters = {}

#----FIGHTER COMMANDS----
def create_fighter_name():
    name = input("What will you call your fighter?\n> ")
    fighters[name] = create_fighter(name)

def fighter_line_up():
    print("Your fighter line-up")
    for i, creature in zip_longest(range(6), fighters):
        print(f"{i + 1}: {creature}")

def check_fighter():
    check_creature = input(" \nWhich fighter would you like to check? (name)\n> ")
    fighters[check_creature].check_stats()


def find_fighter():
    if (population := len(fighters)) < 6:
        create_fighter_name()
    
    elif population >= 6:
        print(" \nYour fighter line-up is full!\nPlease choose a fighter to replace! (name)")
        print("If you do not want to replace a fighter, type 'No'!")
        fighter_line_up()
        replacing = True
        while replacing:
            replace_f = input("> ")
            if replace_f.upper() == 'NO':
                break
            else:
                confirmation = input("Are you sure?\n> ")
                if confirmation.upper() == 'YES':
                    new_fighter_name = input(" \nWhat will be your fighters name?\n> ")
                    replace_fighter(new_fighter_name, replace_f)
                    break
                if confirmation.upper() == 'NO':
                    print("Who will you replace?")


def explore():
    print("You went exploring...")
    chance_of_encounter = random.randint(1, 100)
    if chance_of_encounter <= 75:
        do_encounter()
        confirmation = input(" \nDo you want to attempt to catch it?\n> ")
        if confirmation.upper() == 'YES':
                chance_of_catch = random.randint(1, 100)
                if chance_of_catch <= 75:
                    print("You caught the wild animal!")
                    find_fighter()
                else:
                    print("You lost the animal!")


**def do_encounter():
    type_of_creature_chance = random.randint(1, 3)
    if type_of_creature_chance == 1 or 2 or 3:
        print("You were ambushed by a creature!")
        enemy = Enemy_Fighter()
        enemy.create_stats()
    return enemy.species, enemy.level**

我认为问题出在“do_encounter():”函数和 create_fighter(name: str) 函数中。任何帮助将不胜感激,谢谢:)

我还尝试在“do_encounter():”函数中使用敌人的种类和等级创建固定变量,但它使用相同的结果 nameError: name 'species' is not defined.

function class reference nameerror
© www.soinside.com 2019 - 2024. All rights reserved.