简体   繁体   中英

Issue with a dictionary duplicating itself

So, when I execute the code it print two different dictionaries, with similar objects (but different probably since the object index is different). Here is both scripts. Ignore everything in both scripts apart from init constructor, Ability.add_ability(), and Entity.learn_abilities(). I have thought of posting the entire scripts for better overview, at the bottom of Ability.py there are some statements I have made for testing, which results in two dictionaries, called learned_abilities which have similar but differently indexed objects. The problem is that I only need one. Thanks in advance to anyone. I am a beginner: Ability.py:

from Entity import *
class Ability():
    def __init__(self, ability_attributes):
        self.ability_attributes = ability_attributes

    def set_ability(self):
        self.name = self.ability_attributes[0]
        self.type = self.ability_attributes[1]
        self.element = self.ability_attributes[2]
        self.power = self.ability_attributes[3]
        self.description = self.ability_attributes[4]

    def execute_ability(self, owner, opponent):
        if self.type == "Black":
            if self.element == "Fire":
                total_damage = (self.power * owner.mag) / opponent.fireres
            elif self.element == "Ice":
                total_damage = (self.power * owner.mag) / opponent.iceres
            elif self.element == "Lightning":
                total_damage = (self.power * owner.mag) / opponent.lightres
            elif self.element == "Wind":
                total_damage = (self.power * owner.mag) / opponent.windres
            elif self.element == "Physical":
                total_damage = (self.power * owner.mag) / opponent.magres
            opponent.hp -= total_damage
        if self.type == "White":
            if self.name == "Cure" or self.name == "Cura" or self.name == "Curaga":
                total_healed = self.power * owner.spirit
            opponent.hp += total_healed
        if self.type == "Support":
            if self.name == "Haste":
                opponent.speed *= 2
            if self.name == "Slow":
                opponent.speed /= 2
            if self.name == "Protect":
                opponent.dif *= 1.5
            if self.name == "Might":
                opponent.atk *= 1.5
            if self.name == "Shell":
                opponent.fireres *= 2
                opponent.iceres *= 2
                opponent.lightres *= 2
                opponent.windres *= 2
                opponent.magres *= 2

    def add_ability(self, learner):
        learner.learn_abilities(self)

fire = ["Fire", "Black", "Fire", 90, "Strike with small fire damage."]
blizzard = ["Blizzard", "Black", "Ice", 90, "Strike with small ice damage."]
thunder = ["Thunder", "Black", "Lightning", 90, "Strike with small lightning damage"]
cure = ["Cure", "White", None, 90, "Restore a small amount of hp."]
haste = ["Haste", "Support", None, 0, "Double target speed."]

player_stats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]
player = Entity(player_stats)
Fire = Ability(fire)
Fire.set_ability()
Fire.add_ability(player)
Blizzard = Ability(blizzard)
Blizzard.set_ability()
Blizzard.add_ability(player)
print(player.learned_abilities)

Entity.py:

class Entity():
    def __init__(self, stats):
        self.stats = stats
        self.learned_abilities = {}

    def setting_stats(self):
        self.hp = self.stats[0]
        self.atk = self.stats[1]
        self.dif = self.stats[2]
        self.speed = self.stats[3]
        self.mag = self.stats[4]
        self.fireres = self.stats[5]
        self.iceres = self.stats[6]
        self.lightres = self.stats[7]
        self.windres = self.stats[8]
        self.magres = self.stats[9]
        self.spirit = self.stats[10]

    def learn_abilities(self, new_ability):
        self.learned_abilities[new_ability.name] = new_ability

    def battle_input(self):
        player_input = """
        1. Attack     2. Defend
        3. Black Magics   4.White Magics
        5. Support Magics"""


from Ability import *

Seems to be solved in the comments but I'll leave an answer here in case someone else comes looking. You might want to have a look at the answers here to see how to avoid circular imports.

Basically though, for your current code, removing the following line from Entity.py seems to have fixed the issue, as the problem was being caused by circular imports:

from Ability import *

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