Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一本字典自我复制的问题_Python_Python 3.x - Fatal编程技术网

Python 一本字典自我复制的问题

Python 一本字典自我复制的问题,python,python-3.x,Python,Python 3.x,因此,当我执行代码时,它会打印两个不同的字典,它们具有相似的对象(但可能不同,因为对象索引不同)。下面是两个脚本。忽略这两个脚本中除init构造函数、Ability.add_Ability()和Entity.learn_abilities()之外的所有内容。 为了更好地进行概述,我曾考虑发布整个脚本,在Ability.py的底部有一些我为测试所做的陈述,这导致了两个字典,称为learned_abilities,它们具有相似但不同的索引对象。问题是我只需要一个。 提前感谢所有人!我是初学者! Ab

因此,当我执行代码时,它会打印两个不同的字典,它们具有相似的对象(但可能不同,因为对象索引不同)。下面是两个脚本。忽略这两个脚本中除init构造函数、Ability.add_Ability()和Entity.learn_abilities()之外的所有内容。 为了更好地进行概述,我曾考虑发布整个脚本,在Ability.py的底部有一些我为测试所做的陈述,这导致了两个字典,称为learned_abilities,它们具有相似但不同的索引对象。问题是我只需要一个。 提前感谢所有人!我是初学者! 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 *

在评论中似乎解决了这个问题,但我会在这里留下一个答案,以防其他人来看。您可能想看看答案,看看如何避免循环导入

但是,对于您当前的代码,从Entity.py中删除以下行似乎已经解决了这个问题,因为问题是由循环导入引起的:

from Ability import *

欢迎来到StackOverflow。你没有写什么是“提到的错误”;-)<代码>{'Fire':,'Blizzard':}这是我从运行您的代码中得到的,它是一个包含2个键:值对的字典。您的意思是什么,我不认为导入功能是必要的。你能展示一下你得到的两个字典吗?是的,你不应该让循环导入我在测试中删除了它。我想提一下关于通配符导入的问题:“通配符导入(
来自导入*
)应该避免,因为它们使名称空间中存在的名称变得不清楚,从而混淆了读者和许多自动化工具。“我同意,理想情况下,总是尽量避免通配符导入