- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我制作了一个游戏,并试图为玩家创建一个清单,并且我无法更改名为“ equipped_weapon”的变量。我已经尝试了所有方法,或者得到了异常,或者在打印玩家的清单时显示了先前的武器。我也是初学者,所以如果我想念一些东西,请告诉我。
# Existing items
# Need a funtion to create a random item with random stats and a random name within a certain range.
yes_list = ["yes", "yeah", "sure", "why not"]
add_attack = "Attack:"
add_defense = "Defense:"
add_HP = "HP added:"
rarity = "Rarity"
weapon_rusty_sword = {
"Name:": "Rusty Sword",
add_attack: 1,
add_defense: 0,
rarity: 1
}
weapon_sword_wooden = {
"Name:": "Wooden Sword",
add_attack: 1.5,
add_defense: 0,
rarity: 1
}
weapon_sword_iron = {
"Name:": "Iron Sword",
add_attack: 2,
add_defense: 0,
rarity: 2
}
armor_chest_rusty_mail = {
"Name:": "Rusty Mail",
add_attack: 0,
add_defense: 1,
rarity: 1
}
armor_legs_adventurers = {
"Name:": "Adventurer's Leggings",
add_attack: 0,
add_defense: 1,
rarity: 1
}
armor_legs_rash_leggings = {
"Name:": "Rash Leggings",
add_attack: 0,
add_defense: 0.5,
rarity: 1
}
armor_head_rusty_cap = {
"Name:": "Rusty Cap",
add_attack: 0,
add_defense: 0.5,
rarity: 1
}
potion_hp = {
"Name:": "HP Potion,",
add_HP: 4
}
class Person:
#global equipped_weapon
equipped_weapon = weapon_rusty_sword
ui_equipped_weapon = {"Equipped Weapon: {}".format(weapon_rusty_sword): ""}
equipped_armor = {
"Head Protection:": {"Name": armor_head_rusty_cap["Name:"], "Defense": armor_head_rusty_cap[add_defense]},
"Chest Protection:": {"Name": armor_chest_rusty_mail["Name:"], "Defense": armor_chest_rusty_mail[add_defense]},
"Legs Protection:": {"Name": armor_legs_rash_leggings["Name:"], "Defense": armor_legs_rash_leggings[add_defense]},
}
ATTACK = 1 + equipped_weapon[add_attack]
DEFENSE = 1
HP = 20
gold = 10
potions = {"Potions: ": [potion_hp["Name:"]]}
ui_gold = {"Gold: ": gold}
def __init__(self):
self.name = "Cavalex"
inv = [
equipped_armor,
ui_equipped_weapon,
potions,
ui_gold
]
def see_inventory(self):
for element in self.inv:
for k, v in element.items():
if type(v) == list:
print(k, ' '.join(v))
else:
print(k, v)
# def equip_weapon(self, new_weapon_code):
# global equipped_weapon
# eq_val = input(
# "Do you want to equip this weapon? ->( {} )<-\nNote that your current weapon ( {} )will be discarded.".format(new_weapon_code["Name:"], self.equipped_weapon["Name:"]))
# if eq_val.lower() in yes_list:
# #del self.equipped_weapon
# self.equipped_weapon = new_weapon_code
# print("The weapon you had was discarded.")
# else:
# print("The new weapon was discarded.")
# See total amount of defense.
def defense_points():
return sum(value["Defense"] for key, value in player.equipped_armor.items())
def add_gold(amount):
player.gold += amount
# Adding to inventory
def new_potion_to_inv(potion):
player.potions["Potions: "].append(potion["Name:"])
def equipp_weapon(new_weapon_code):
# global player.equipped_weapon
eq_val = input(
"Do you want to equip this weapon? ->( {} )<-\nNote that your current weapon ( {} )will be discarded.".format(
new_weapon_code["Name:"], player.equipped_weapon["Name:"]))
if eq_val.lower() in yes_list:
del player.equipped_weapon
player.equipped_weapon = new_weapon_code
print("The weapon you had was discarded.")
else:
print("The new weapon was discarded.")
player = Person()
# game loop
while True:
print("Your name: ", player.name)
player.see_inventory() # Can't put print this function, else will print "None" in the end.
print("\nThis is your total armor defense: {}".format(defense_points()))
print()
new_potion_to_inv(potion_hp)
player.see_inventory()
print(player.ATTACK)
print()
print("Now we are changing your weapon.")
equipp_weapon(weapon_sword_iron)
print()
print(player.ATTACK)
player.see_inventory()
break
C:\Users\mateu\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py
Your name: Cavalex
Head Protection: {'Name': 'Rusty Cap', 'Defense': 0.5}
Chest Protection: {'Name': 'Rusty Mail', 'Defense': 1}
Legs Protection: {'Name': 'Rash Leggings', 'Defense': 0.5}
Equipped Weapon: {'Name:': 'Rusty Sword', 'Attack:': 1, 'Defense:': 0, 'Rarity': 1}
Potions: HP Potion,
Gold: 10
This is your total armor defense: 2.0
Head Protection: {'Name': 'Rusty Cap', 'Defense': 0.5}
Chest Protection: {'Name': 'Rusty Mail', 'Defense': 1}
Legs Protection: {'Name': 'Rash Leggings', 'Defense': 0.5}
Equipped Weapon: {'Name:': 'Rusty Sword', 'Attack:': 1, 'Defense:': 0, 'Rarity': 1}
Potions: HP Potion, HP Potion,
Gold: 10
2
Now we are changing your weapon.
Do you want to equip this weapon? ->( Iron Sword )<-
Note that your current weapon ( Rusty Sword )will be discarded.yes
Traceback (most recent call last):
File "C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py", line 158, in <module>
equipp_weapon(weapon_sword_iron)
File "C:/Users/mateu/PycharmProjects/Trabalhos_Python/TESTING_THINGS/testing_armor_without_weapons_and_armor.py", line 139, in equipp_weapon
del player.equipped_weapon
AttributeError: equipped_weapon
Process finished with exit code 1
最佳答案
问题是您将equipped_weapon
设置为类属性,而不是实例属性。 del instance.attribute
仅删除实例属性,甚至不查找类属性。
如果目标只是更改实例,则可以完全删除del
;否则,只需删除del
。下一行将添加(如果不存在实例属性,则将class属性遮盖)或替换(如果已存在实例属性)实例的特定武器。
如果目标是更改班级,则需要更改分配以对班级进行操作:
type(player).equipped_weapon = new_weapon_code
Player.equipped_weapon = new_weapon_code
del
都是不必要的。分配新值将替换旧值,并以与
del type(player).equipped_weapon
显式相同的方式隐式删除对旧值的引用。用
del Player.equipped_weapon
或
del
专门删除class属性是完全合法的,但这是没有意义的。无论所需的行为如何,简单地将其分配给class或instance属性都将丢弃旧的引用。
see_inventory
可以防止异常,但是您的输出(来自
see_inventory
)永远不会改变。这是因为
Player.inv
正在查看
Player.ui_equipped_weapon
,而后者又包含对
ui_equipped_weapon
原始值的引用。问题是,虽然
equipped_weapon
和
equipped_weapon
是基于相同的默认武器初始化的,但它们不会彼此同步。更改一个对另一个没有影响(因此,在类或实例上更改
inv
不会影响
see_inventory
,因此
property
永远不会更改)。
dict
,它们会根据另一个属性动态地重新计算其值,从而防止它们不同步。
import copy
# Factored out to avoid repetition
def clean_armor(armor):
return {k.rstrip(':'): v
for k, v in armor.items() if k in {'Name:', 'Defense:'}}
class Person:
DEFAULT_WEAPON = weapon_rusty_sword
DEFAULT_ARMOR = {
"Head Protection:": clean_armor(armor_head_rusty_cap),
"Chest Protection:": clean_armor(armor_chest_rusty_mail),
"Legs Protection:": clean_armor(armor_legs_rash_leggings),
}
def __init__(self, name="Cavalex",
equipped_weapon=DEFAULT_WEAPON, equipped_armor=DEFAULT_ARMOR,
base_attack=1, base_defense=1,
hp=20, gold=10,
potions=(potion_hp["Name:"],)):
self.name = name
# deepcopy is defensive (so changing defaults doesn't retroactively
# alter existing instance); can be removed if you guarantee defaults
# won't be changed, or you want changes to defaults to affect
# Players still wielding default items
self.equipped_weapon = copy.deepcopy(equipped_weapon)
self.equipped_armor = copy.deepcopy(equipped_armor)
self.base_attack = int(base_attack)
self.base_defense = int(base_defense)
self.HP = int(hp)
self.gold = int(gold)
# potions only used as dictionary, but it's silly to store it as one
# Just store list of potion names in protected attribute,
# property can construct the expected dict on demand
self._potions = list(potions)
@property
def ATTACK(self):
return self.base_attack + self.equipped_weapon[add_attack]
@property
def DEFENSE(self):
return self.base_defense + sum(armor['Defense'] for armor in self.equipped_armor.values())
@property
def potions(self):
return {"Potions: ": self._potions}
@property
def ui_gold(self):
return {"Gold: ": self.gold}
@property
def ui_equipped_weapon(self):
return {"Equipped Weapon: {}".format(self.equipped_weapon): ""}
@property:
def inv(self):
return [
self.equipped_armor,
self.ui_equipped_weapon,
self.potions,
self.ui_gold,
]
def see_inventory(self):
for element in self.inv:
for k, v in element.items():
if isinstance(v, list):
print(k, ' '.join(v))
else:
print(k, v)
property
中似乎选择了怪异的键名使显示更容易,但使所有非显示使用起来更烦人;使用顶级功能对于仅作为实例方法等有意义的内容),但我保留了这些古怪之处,以使其成为您现有类的直接替代品(但在实例上设置了所有按玩家属性,而不是在类上设置,通过
player
计算而不是静态设置的派生属性,因为独立的静态属性会增加基本属性和派生属性不同步的几率。
name
的行为,尽管
__init__
实际上应该是一个非可选的参数(除非“ Cavalex”是一个通用名称,您可以放心地假设大部分或全部)玩家的名字)。
property
并定义
class
似乎有点乏味,但是就代码而言,您实际上可以有意义的方式使用而无需极端重复(以及错别字和逻辑错误的伴随风险),了解行为等,这是唯一有意义的方法;一个具有99%类属性的
class
(所有方法都有效地忽略了每个实例状态)也可能只是一堆全局变量和顶级函数,而不是
关于python - 无法在Python中使用外部函数更改类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52335181/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How to nest OR statements in JavaScript? 有没有办法做到这一点:
在 JavaScript 中有没有办法让一个变量总是等于一个变量?喜欢var1 = var2但是当var2更新,也是var1 . 例子 var var1 = document.getElementBy
我正在努力理解这代表什么 var1 = var2 == var3 我的猜测是这等同于: if (var2 == var3): var1 = var2 最佳答案 赋值 var1 = var2
这个问题已经有答案了: What does the PHP error message "Notice: Use of undefined constant" mean? (2 个回答) 已关闭 8
我在临时表中有几条记录,我想从每条记录中获取一个值并将其添加到一个变量中,例如 color | caption -------------------------------- re
如何将字符串转为变量(字符串变量--> $variable)? 或者用逗号分隔的变量列表然后转换为实际变量。 我有 2 个文件: 列名文件 行文件 我需要根据字符串匹配行文件中的整行,并根据列名文件命
我有一个我无法解决的基本 php 问题,我也想了解为什么! $upperValueCB = 10; $passNodeMatrixSource = 'CB'; $topValue= '$uppe
这可能吗? php $variable = $variable1 || $variable2? 如果 $variable1 为空则使用 $variable2 是否存在类似的东西? 最佳答案 PHP 5
在 Perl 5.20 中,for 循环似乎能够修改模块作用域的变量,但不能修改父作用域中的词法变量。 #!/usr/bin/env perl use strict; use warnings; ou
为什么这不起作用: var variable; variable = variable.concat(variable2); $('#lunk').append(variable) 我无法弄清楚这一点
根据我的理解,在32位机器上,指针的sizeof是32位(4字节),而在64位机器上,它是8字节。无论它们指向什么数据类型,它们都有固定的大小。我的计算机在 64 位上运行,但是当我打印包含 * 的大
例如: int a = 10; a += 1.5; 这运行得很完美,但是 a = a+1.5; 此作业表示类型不匹配:无法从 double 转换为 int。所以我的问题是:+= 运算符 和= 运算符
您好,我写了这个 MySQL 存储过程,但我一直收到这个语法错误 #1064 - You have an error in your SQL syntax; check the manual that
我试图在我的场景中显示特定的奖牌,这取决于你的高分是基于关卡的目标。 // Get Medal Colour if levelHighscore goalScore { sc
我必须维护相当古老的 Visual C++ 源代码的大型代码库。我发现代码如下: bIsOk = !!m_ptr->isOpen(some Parameters) bIsOk的数据类型是bool,is
我有一个从 MySQL 数据库中提取的动态产品列表。在 list 上有一个立即联系 按钮,我正在使用一个 jquery Modal 脚本,它会弹出一个表单。 我的问题是尝试将产品信息变量传递给该弹出窗
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
jQuery Core Style Guidelines建议两种不同的方法来检查变量是否已定义。 全局变量:typeof variable === "undefined" 局部变量:variable
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: “Variable” Variables in Javascript? 我想肯定有一种方法可以在 JavaScrip
在语句中使用多重赋值有什么优点或缺点吗?在简单的例子中 var1 = var2 = true; 赋值是从右到左的(我相信 C# 中的所有赋值都是如此,而且可能是 Java,尽管我没有检查后者)。但是,
我是一名优秀的程序员,十分优秀!