2015-06-26 46 views
-1

我已經將這段代碼放入了一大段代碼中,但是我將其縮小爲這個錯誤。我知道這個錯誤是我正試圖在字典中添加一個變量。有什麼方法可以將其添加到實際統計數據本身?如何將數字添加到字典中的變量?

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 
+0

你能後的實際的錯誤信息? – michaelpri

+0

錯誤消息: 回溯(最近最後調用): 文件 「F:/TARG/temp.py」,第22行,在 選擇+ = pointDeduction 類型錯誤:不能連接 'STR' 和 'INT' 對象 – TyTheChosenOne

+0

搜索該錯誤消息「TypeError:無法連接'str'和'int'對象」,會直接導致各種(StackOverflow)相關答案和解決方案。 – Evert

回答

0

將您的統計信息收集在一個字典中,然後像這樣使用。

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) 
+0

我如何打印statlist中的項目?所以它會顯示(例如):小槍= 5 – TyTheChosenOne

0

你的榜樣目前沒有字典。你已經啓動它錯了。它應該是如下:

statlist = {"attribute_name" : attribute_value, REPEAT} 

一旦你有了正確的字典initalization

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) 

也有一些邏輯問題的分發點的時候,但你可以明白這一點你自己。

0

我從你的代碼猜測,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) 
+2

這不是字典的正確初始化。那不會運行。 – AndrewGrant

+0

非常正確,我從他的代碼頂部複製了東西,將刪除'='符號! – maccartm