Python 变量在每个循环中更新,即使我在循环外初始化它

Python variable is being updated on each loop even though I initialized it outside of the loop

正在 python 中制作一个 Fallout 小游戏...

我有一个叫做level_up的功能,允许玩家分配技能点。

玩家的当前统计数据存储为玩家的属性 class。

我在 level_up 函数中创建了一个变量,用于复制玩家的当前统计数据,以便在升级循环为 运行 时与之进行比较。我这样做是为了让玩家无法将统计值编辑为低于升级时的值。

我在函数的主循环之外初始化了这个变量,以使其保持不变,但是随着玩家对他们的统计数据进行编辑,这个变量(原始值)似乎被更新为新值,而不是停留在升级发生时的值。

例子 (Billy 升级时的 Small Guns 技能是 15。original_values 应该存储 15 是原始值。Billy 为 Small Guns 增加 5 点,使它成为 20。他决定他想回到 15。应该工作由于原始值为 15,但 original_values 现在小型枪支为 20,因此不会发生此更改。

我认为在循环外初始化 original_values 是我需要做的。这与我正在更新 class 属性有关吗?

谁能告诉我我做错了什么?谢谢。

函数

def level_up(self):
        """Formula true to FO1 and FO2"""
        self.lvl += 1

        # flooring the result may not be true to the original game, but am doing for simplicity's sake
        self.hp += math.floor((self.special['end']/2)) + 2
        
        # create skill points to spend
        points = self.skill_rate
        
        # end loop condition
        confirm = False

        # create variables for the original stat values before level up
        original_values = self.combat_skills

        # needs to allow for editing only current changes, cannot go lower than previous values
        while points != 0 and confirm == False:
            print(f'\nSkill Points: {points}')
            for skill, value in self.combat_skills.items():
                print(f'{skill}: {value}')
            
            # get stat to edit
            while True:
                stat_to_edit = input('STAT TO EDIT (ex. small guns) or DONE to confirm: ').lower()
                if stat_to_edit in self.combat_skills.keys():
                    break
                elif stat_to_edit == 'done':
                    if points != 0:
                        print('Must use all skill points before confirming!')
                        continue
                    else:
                        confirm = True
                        break
                else: print('Invalid Entry')
                continue
            
            # get new value


            while True:
                try:
                    new_value = int(input(f'NEW {stat_to_edit.upper()} VALUE: '))
                except ValueError:
                    print('Value must be numerical (ex. 12)')
                    continue
                # make variable for the difference
                change = new_value - self.combat_skills[stat_to_edit]
                # check new value, make sure its not less than original stat value and there are enough points available
                if new_value < original_values[stat_to_edit]:
                    print(f'Cannot reduce stat past original value ({original_values[stat_to_edit]})')
                    continue
                # check points available against the change between current value and new value, not original value
                if change <= points:
                    # change is valid, set new stat value and subtract points from points bank. (negative change values will increase points)
                    self.combat_skills[stat_to_edit] = new_value
                    points -= change
                    break
                else:
                    print(f'Not enough skill points for this change! You need {change}.')
                    continue

Class

class Player_Character:
    """Represents the player character and all associated stats and possessions. Formulas true to FO1 and FO2"""
    def __init__(self, name, lvl, special):
        self.name = name
        self.lvl = lvl
        self.special = special
        self.hp = 15 + self.special['str'] + (2 * self.special['end'])
        self.ap = 5 + math.floor(self.special['agl']/2)
        self.seq = 2 * self.special['per']
        self.ac = self.special['agl']
        self.carry_weight = 25 + (self.special['str'] * 25)
        self.skill_rate = (self.special['int'] * 2) + 5
        self.weapon = None
        self.armor = None

        # combat skills
        self.small_guns = 35 + self.special['agl']
        self.big_guns = 10 + self.special['agl']
        self.energy_weapons = 10 + self.special['agl']
        # dictionary for use in game and in functions
        self.combat_skills = {'small guns': self.small_guns, 'big guns': self.big_guns, 'energy weapons': self.energy_weapons}

original_values = self.combat_skills 不复制。它只是对同一对象的另一个引用 - 无论您使用哪个引用访问它们,都会看到通过一个引用所做的更改,因为它们都是同一个对象。

如果您想制作不同的副本,请使用 dictcopy 方法制作副本。例如original_values = self.combat_skills.copy()