【发布时间】:2019-08-08 15:38:44
【问题描述】:
我正在通过编写 DnD 风格的 roguelike/地牢爬行游戏自学 Python。我的角色创建和游戏玩法基于 5E SRD。
我已经完成了 Roguebasin TCOD 教程和 Philip Johnson 的文字冒险教程。
我要做的是从输入中获取一个值并将其分配给适当的统计数据,我已将其定义为角色对象的属性。
class BaseCharacter:
def __init__(self):
self.strength = None
self.dexterity = None
self.constitution = None
self.intelligence = None
self.wisdom = None
print("With the standard array, you get these scores to distribute as you see fit: ")
print("15, 14, 13, 12, 10, 8")
values = [15, 14, 13, 12, 10, 8]
stats = ["strength", "dexterity", "constitution", "intelligence",
"wisdom", "charisma"]
ordered_values = []
for s in stats:
print("Which score would you like to assign to {}?"
.format(s))
value = input()
ordered_values.append(value)
values.remove(int(value))
print("Remaining values: " + str(list(values)))
我有一些这样的东西,以便我的代码编译并运行以进行测试。我想遍历统计信息列表并将用户输入分配给相应的统计信息:
Which score would you like to assign to strength?
15
Remaining values: 14, 13, 12, 10, 8
print(self.strength)
15
这似乎是可行的,但到目前为止,我想出的只是将统计信息和输入压缩到元组中。我对如何真正确保将正确的值分配给正确的属性持空白。
【问题讨论】:
-
你知道你可以将这些值传递给你的类的构造函数吗?
-
但这不会破坏循环的目的吗?我最初有
self.s = value,认为会使用s 的当前值。但是 Python 似乎将其解释为我试图将self.s分配为新属性并给出未解决的名称错误。 -
没关系。我没有进一步阅读。 :)
标签: python loops properties