如何在字典中给变量加一个数字?

2024-04-28 23:01:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我的错误消息是

Traceback (most recent call last): File "F:/TARG/temp.py", line 22, in <module> choice += pointDeduction TypeError: cannot concatenate 'str' and 'int' objects

Tags: to代码错误lockpickdopointssciencerepair
3条回答

把你的数据收集起来,然后像这样使用。你知道吗

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)

我从您的代码中猜测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 = {"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)

在分发点时,您也有一些逻辑问题,但您可以自己解决。你知道吗

相关问题 更多 >