Python 如何向字典中的变量添加数字?

Python 如何向字典中的变量添加数字?,python,python-2.7,Python,Python 2.7,我已经把这段代码放进了一个更大的代码块中,但是我把它缩小到了这个错误。我知道错误在于我试图在字典中添加变量。有没有办法把它添加到实际数据中 smallGuns = 5 bigGuns = 2 unarmed = 3 meleeWeapons = 20 throwing = 4 firstAid = 2 sneak = 5 lockpick = 10 steal = 3 science = 4 repair = 3 speech = 5 choice = raw_input("Which sta

我已经把这段代码放进了一个更大的代码块中,但是我把它缩小到了这个错误。我知道错误在于我试图在字典中添加变量。有没有办法把它添加到实际数据中

smallGuns = 5
bigGuns = 2
unarmed = 3
meleeWeapons = 20
throwing = 4
firstAid = 2
sneak = 5
lockpick = 10
steal = 3
science = 4
repair = 3
speech = 5

choice = raw_input("Which stat do you want to add points to?")
skillPoints = 5

statlist = ['small guns', 'big guns', 'unarmed', 'melee weapons', 'throwing', 'first aid' 'sneak', 'lockpick', 'steal', 'science', 'repair', 'speech']

if choice in statlist:
pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
if pointDeduction <= choice:
        choice += pointDeduction
        skillPoints -= pointDeduction
else:
        print "You do not have that many points to distribute to %s." %(choice)

print steal
smallGuns=5
大炮=2
手无寸铁=3
Melee武器=20
投掷=4
急救=2
潜行=5
锁钩=10
偷窃=3
科学=4
修理=3
语音=5
choice=原始输入(“您希望向哪个统计数据添加点数?”)
技能点=5
statlist=[“小枪”、“大枪”、“手无寸铁”、“近战武器”、“投掷”、“急救”、“偷袭”、“开锁”、“偷窃”、“科学”、“修理”、“演讲”]
如果在statlist中选择:
PointDecreation=输入(“您希望向%s添加多少点?(最多%s点)”%(选项,技能点))

如果PointDecreation您的示例当前没有字典。你搞错了。其内容应如下:

statlist = {"attribute_name" : attribute_value, REPEAT}
一旦你有了正确的字典初始化

statlist = {'small guns' : 5, 'big guns' : 2, 'unarmed' : 3} # you do the rest
choice = raw_input("Which stat do you want to add points to?")
if choice in statlist:
    pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
    if pointDeduction <= statlist[choice]:
        statlist[choice] -= pointDeduction
        skillPoints -= pointDeduction
else:
    print "You do not have that many points to distribute to %s." %(choice)
statlist={'small guns':5'big guns':2'unarmed':3}#剩下的由你来做
choice=原始输入(“您希望向哪个统计数据添加点数?”)
如果在statlist中选择:
PointDecreation=输入(“您希望向%s添加多少点?(最多%s点)”%(选项,技能点))

如果PointDecreation我从您的代码中猜测,
statlist
是一个包含
stat
键和
stat value
值的字典。现在你有了一个列表,所以本质上你说的是“如果该项在列表中,请将一个数字连接到它的末尾”(尽管不正确)

您要做的是在问题中添加词典。第一部分,即声明变量的部分,并不完全必要,您可以这样完成:

statlist = {'small guns' : 5, 'big guns' : 2, ...}
对于每个值。然后,要更改统计信息:

if choice in statlist:
    pointDeduction = input("How many points do you wish to add to %s? (Up to %s points)" %(choice, skillPoints))
    if pointDeduction <= statlist[choice]:
        statlist[choice] += pointDeduction
        skillPoints -= pointDeduction
else:
    print "You do not have that many points to distribute to %s." %(choice)
如果在statlist中选择:
PointDecreation=输入(“您希望向%s添加多少点?(最多%s点)”%(选项,技能点))

如果扣分收集你的数据并像这样使用

choice = raw_input("Which stat do you want to add points to?")
skillPoints = 5

statlist = {'small guns': 5, 'big guns': 2, 'unarmed': 3, 'melee weapons': 20, 'throwing':4, 'first aid':2, 'sneak': 5, 'lockpick': 10, 'steal': 3, 'science': 4, 'repair': 3, 'speech': 5}

if choice in statlist:
    pointDeduction = int(raw_input("How many points do you wish to add to %s? (Up to %d points)" %(statlist[choice], skillPoints)))

    if pointDeduction <= skillPoints:
        statlist[choice] += pointDeduction
        skillPoints -= pointDeduction
    else:
        print "You do not have that many points to distribute to %s." %(choice)

    print statlist[choice]
else:
    print 'you entered an invalid choice'

您可以发布实际的错误消息吗?错误消息:回溯(最近一次调用):文件“F:/TARG/temp.py”,第22行,在选项+=PointDecretion TypeError:无法连接'str'和'int'对象搜索该错误消息时,“TypeError:无法连接'str'和'int'对象”,会直接导致各种错误(StackOverflow)相关答案和解决方案。使用Python 2时执行
input()
。这不是字典的正确初始化。这不会运行。非常正确,我从他的代码顶部复制了内容,将删除
=
符号!我现在如何打印statlist中的项目?以便它显示(例如):小型火炮=5
choice = raw_input("Which stat do you want to add points to?")
skillPoints = 5

statlist = {'small guns': 5, 'big guns': 2, 'unarmed': 3, 'melee weapons': 20, 'throwing':4, 'first aid':2, 'sneak': 5, 'lockpick': 10, 'steal': 3, 'science': 4, 'repair': 3, 'speech': 5}

if choice in statlist:
    pointDeduction = int(raw_input("How many points do you wish to add to %s? (Up to %d points)" %(statlist[choice], skillPoints)))

    if pointDeduction <= skillPoints:
        statlist[choice] += pointDeduction
        skillPoints -= pointDeduction
    else:
        print "You do not have that many points to distribute to %s." %(choice)

    print statlist[choice]
else:
    print 'you entered an invalid choice'
# print an individual entry
print 'My small guns points are %d' % statlist['small guns']

# print all entries in a loop
print 'My points inventory is'
for key, value in statlist.iteritems():
    print '%s = %d' % (key, value)