2017-03-10 51 views
-1

在我開始之前,讓我說這是我第一次單獨編寫代碼,而且我對編碼還比較陌生。我現在只做了幾個星期的python。不支持的操作數類型(s) - =:'list'和'int'

所以我有一個學校項目,老師讓我們做我們想做的事情,只是我們展示了我們在學習IT之前學到的東西。我正在嘗試製作一份俄勒岡小徑的副本,並且考慮到我已經訴諸因特網,它不會太好(不是說這個網站上的人不太好)。我的代碼目前看起來是這樣的:

resources = { 
    "food": [10], 
    "energy": [100], 
    "distance": [2000], 
    "exhaustion": [0], 
    "hunger": [0] 

} 

def prompt(): 
    print "You can eat, move, hunt, or rest." 

def get_action(): 
    return raw_input("What will you do?") 

def what_do(action): 
    for number in resources: 
     print number + str(resources[number]) 
    if action == "eat": 
     print "You ate. Hunger restored." 
     resources['hunger'] == 0 
     resources['food'] -= 1 
    if action == "hunt": 
     print "You went out and hunted. You found 10 food." 
     resources['food'] += 10 
     print resources['food'] 
    if action == "move": 
     print "You moved 50 miles." 
     resources['distance'] -= 50 
    if action == "rest": 
     print "You rested. Energy restored" 
     resources['energy'] == 100 
     resources['exhaustion'] == 0 

def game(): 
    while resources['distance'] >= 0: 
     prompt() 
     action = get_action() 
     what_do(action) 

game() 

在只有一個動作運作的瞬間,而這就是rest行動。我假設這是因爲沒有真正的運營商,它只是刪除我的疲憊回到0無論它的工作如預期,我不能說,因爲輸出保持不變。與其他所有的動作,我收到以下錯誤消息的變體:

Traceback (most recent call last): 
    File "python", line 46, in <module> 
    File "python", line 44, in game 
    File "python", line 27, in what_do 
TypeError: unsupported operand type(s) for -=: 'list' and 'int' 

是(30,如果你想知道34)中列出的第三行改變各個輸出的唯一的東西,和運營商的變化到+。我會重申,我對編碼非常陌生,所以我不知道這意味着什麼。任何幫助表示讚賞。

回答

2

你的問題是資源值被存儲爲列表ie。 "food": [10] [10]是一個列表,因爲它是在方括號。更改資源,這使其工作:

resources = { 
    "food": 10, 
    "energy": 100, 
    "distance": 2000, 
    "exhaustion": 0, 
    "hunger": 0 

} 

其他,你需要指定使用=變量,而不是==因爲==用於比較和返回真或假。所以what_do函數應該是這樣的:

def what_do(action): 
    for number in resources: 
print number + str(resources[number]) 
if action == "eat": 
    print "You ate. Hunger restored." 
    resources['hunger'] = 0 
    resources['food'] -= 1 
if action == "hunt": 
    print "You went out and hunted. You found 10 food." 
    resources['food'] += 10 
    print resources['food'] 
if action == "move": 
    print "You moved 50 miles." 
    resources['distance'] -= 50 
if action == "rest": 
    print "You rested. Energy restored" 
    resources['energy'] = 100 
    resources['exhaustion'] = 0 
0

使用

資源= { 「食物」:10, 「能量」:100, 「距離」:2000, 「用盡」:0, 「飢餓」:0

}

而不是

0

resources包含作爲值的列表。

resources['hunger']例如用單個元素返回列表。當你從列表中減去一個數字時,你期望發生什麼?

我不認爲你打算resources是一個列表的地圖。只需從resources中的值中刪除[]以使數值而不是列表。

0

你試着基本上做到

[2000] - 50 

,因爲[2000]是一個列表,它是不可能的。取而代之的

food: [10], 

使用

food: 10, 

等。如果你必須使用列表,然後做你的減法是這樣的:

resources['distance'][0] -= 50 

,其訪問資源的第一個元素[「距離」]這是一個列表的第一個元素

-1

您最初的資源是用文辭list s作爲值。你可能只想使用數字。

resources = { 
    "food": 10, 
    "energy": 100, 
    "distance": 2000, 
    "exhaustion": 0, 
    "hunger": 0 
} 

接下來,您在what_do中的某些更新有誤。您應該使用=更新資源的價值,而不是==如:

if action == "rest": 
    print "You rested. Energy restored" 
    resources['energy'] = 100 
    resources['exhaustion'] = 0 
0

改變資源

resources = {"food":10,"energy":100,"distance:2000,"exhaustion": 0,"hunger":0} 

和更改所有的「==」到「=」者除外,如果聲明。

相關問題