我的函数的数学结果是错误的,我不知道为什么

时间:2021-07-14 02:11:57

标签: python

这段代码应该从我的字典中获取几个值,然后更新同一个字典中的另一个值。多亏了一些帮助,我得到了她的帮助。代码如下所示:

        def attack(enemy):
            text_1 = enemy["text_1"]
            text_2 = enemy["text_2"]
            strength = enemy["strength"]
            weapon_damage = enemy["weapon damage"]
            attack_modifier = enemy["attack modifier"]
            health = enemy["health"]
            damage_to_enemy = strength * weapon_damage * randint(2, 5) * attack_modifier
            print(f"{text_1} {damage_to_enemy} {text_2}")
            enemy.update(health=(health - damage_to_enemy))
            print(health)
        attack(character_stats_dictionary)
        attack(enemy_1_stats_dictionary)

不幸的是,输出中的数学是错误的:

attack
You did 104 damage to the enemy
1000
the enemy did 100 damage to you
1000
Input attack, inventory or stats
attack
You did 208 damage to the enemy
896
the enemy did 100 damage to you
900
Input attack, inventory or stats
attack
You did 104 damage to the enemy
688
the enemy did 40 damage to you
800
Input attack, inventory or stats
attack
You did 156 damage to the enemy
584
the enemy did 60 damage to you
760
Input attack, inventory or stats
attack
You did 104 damage to the enemy
428
the enemy did 40 damage to you
700
Input attack, inventory or stats
attack
You did 104 damage to the enemy
324
the enemy did 60 damage to you
660
Input attack, inventory or stats
attack
You did 104 damage to the enemy
220
the enemy did 60 damage to you
600
Input attack, inventory or stats
attack
You did 104 damage to the enemy
116
the enemy did 40 damage to you
540
Input attack, inventory or stats
attack
You did 260 damage to the enemy
12
the enemy did 60 damage to you
500

以下是我正在使用的两本词典,以供参考:

character_stats_dictionary = {"health": 1000, "strength": 13,
                              "magic": 22, "defense": 1, "weapon damage": 1,
                              "attack modifier": 1, "text_1": "You did", "text_2": "damage to the enemy"}

enemy_1_stats_dictionary = {"health": 1000, "strength": 4, "defense": 3, "weapon damage": 5,
                            "attack modifier": 1, "text_1": "the enemy did", "text_2": "damage to you"}

0 个答案:

没有答案