【发布时间】:2018-07-30 00:55:23
【问题描述】:
attributes = {"Health": 0, "Strength": 0, "Dexterity": 0,
"Intelligence": 0}
points = 40
def add_points(attributes):
type = input("Which attribute would you like to adjust?: ")
if type in attributes:
how_many = int(input("Add how many points?: "))
if how_many <= points:
type += how_many
return type
add_points(attributes)
一些上下文。上面的代码是我正在构建的角色创建程序的一部分。我已经编写了整个程序,但是我想使用一个函数而不是键入一堆循环。试图让我的代码更简洁和 Pythonic。
我无法弄清楚如何获取用户选择的任何键的值并将 x 点数(也是用户选择的)添加到分配给所述键的值。
任何帮助将不胜感激!
【问题讨论】:
-
attributes[type]+= how_many. -
P.S:只是一个旁注,避免在python中使用
type关键字作为变量名。 -
madjaoue,为什么会有这样的约定?
-
您也可以使用
dict.fromkeys()仅从名称(以及相同的 0 值)创建字典 -
对于使用
type,可能你可以在stackoverflow.com/a/10568115/5916727查看类似的讨论
标签: python function dictionary input