2016-10-04 53 views
0

我正在爲我正在編寫的程序構建一些目錄,但是在遊戲中使用時並且沒有使用它們時,它們會一直出錯。每行都拋出相同的錯誤(Syntax Error: invalid syntax),我想知道爲什麼。線是:目錄 - 爲什麼這個錯誤?

secretpassageunearthed = 'No' 
pickedup = {'broom': '0', 'rope': '0', 'torch': '0', 'coffin': '0', 'skeleton' = '0', 'key2': '0', 'rock': '0', 'key1': '0', 'mailbox': '0', 'instructions': '0', 'letter': '0', 'key3': '0', 'oar': '0', 'boat': '0', 'red button': '0', 'eaglenest': '0', 'faberge egg': '0', 'trophy case': '0', 'facial': '0', 'gun': '0', 'safe': '0', 'diamond ring': '0', 'junk': '0', 'combination': '0', 'egg': '0', 'flashlight': '0', 'cork': '0', 'bottle': '0', 'oil': '0', 'crown': '0', 'shovel': '0', 'knob': '0', 'twinkies': '0', 'scanner': '0', 'toychest': '0'} 
#has the player been in the room? 
beenin = {'frontdoor': '0', 'forest': '0', 'forest2': '0', 'forest3': '0', 'forest4': '0', 'lakeshore': '0', 'garden': '0', 'shed': '0', 'lake': '0', 'farshore': '0', 'cliff1': '0', 'cliff2': '0', 'cliff3': '0', 'cliff4': '0', 'clifftop': '0', 'frontsteps': '0', 'frontlawn': '0', 'gate': '0', 'sittingroom': '0', 'livingroom': '0', 'pantry': '0', 'foyer': '0', 'trophyroom': '0', 'hall1': '0', 'diningroom': '0', 'kitchen': '0', 'elevatorup': '0', 'hall3': '0', 'hall2': '0', 'masterbed': '0', 'closet': '0', 'nursery': '0', 'bath': '0', 'guestroom': '0', 'study': '0', 'secretroom': '0', 'servantdining': '0', 'servantbath': '0', 'servantroom': '0', 'boilerroom': '0', 'mechanicalroom': '0', 'winecellar': '0', 'deadend': '0', 'torturechamber': '0', 'morgue': '0'} 
maxweight = 50 
weights = {'broom': 5, 'rope': 8, 'torch': 5, 'coffin': 40, 'skeleton': 9, 'key1': 2, 'key2': 1, 'rock': 4, 'instructions': 0, 'mailbox': 60, 'letter': 1, 'gargoyle': 60, 'key3': 1, 'blockade': 60, 'oar': 5, 'boat': 30, 'button': 1, 'eagle nest': 60, 'faberge egg': 10, 'trophy case': 60, 'book': 3, 'facial': 1, 'gun': 3, 'safe': 60, 'diamondring': 5, 'junk': 1, 'combo': 1, 'egg': 1, 'flashlight': 3, 'oil': 4, 'bottle': 3, 'cork': 1, 'chandelier': 60, 'crown': 5, 'shovel': 5, 'knob': 3, 'twinkies': 2, 'pot of gold': 30, 'painting': 20, 'scanner': 60, 'toychest': 55} 

任何幫助他們爲什麼他們錯誤了,將不勝感激。

+2

''skeleton'='0''在Python的dict文字中不是合法的語法。 – skovorodkin

+0

另外,這些是_dictionaries_,而不是_directories_。 –

回答

0

不能使用=在字典

變化=分配給:,這應該工作:

secretpassageunearthed = 'No' 
pickedup = {'broom': '0', 'rope': '0', 'torch': '0', 'coffin': '0', 'skeleton' : '0', 'key2': '0', 'rock': '0', 'key1': '0', 'mailbox': '0', 'instructions': '0', 'letter': '0', 'key3': '0', 'oar': '0', 'boat': '0', 'red button': '0', 'eaglenest': '0', 'faberge egg': '0', 'trophy case': '0', 'facial': '0', 'gun': '0', 'safe': '0', 'diamond ring': '0', 'junk': '0', 'combination': '0', 'egg': '0', 'flashlight': '0', 'cork': '0', 'bottle': '0', 'oil': '0', 'crown': '0', 'shovel': '0', 'knob': '0', 'twinkies': '0', 'scanner': '0', 'toychest': '0'} 
#has the player been in the room? 
beenin = {'frontdoor': '0', 'forest': '0', 'forest2': '0', 'forest3': '0', 'forest4': '0', 'lakeshore': '0', 'garden': '0', 'shed': '0', 'lake': '0', 'farshore': '0', 'cliff1': '0', 'cliff2': '0', 'cliff3': '0', 'cliff4': '0', 'clifftop': '0', 'frontsteps': '0', 'frontlawn': '0', 'gate': '0', 'sittingroom': '0', 'livingroom': '0', 'pantry': '0', 'foyer': '0', 'trophyroom': '0', 'hall1': '0', 'diningroom': '0', 'kitchen': '0', 'elevatorup': '0', 'hall3': '0', 'hall2': '0', 'masterbed': '0', 'closet': '0', 'nursery': '0', 'bath': '0', 'guestroom': '0', 'study': '0', 'secretroom': '0', 'servantdining': '0', 'servantbath': '0', 'servantroom': '0', 'boilerroom': '0', 'mechanicalroom': '0', 'winecellar': '0', 'deadend': '0', 'torturechamber': '0', 'morgue': '0'} 
maxweight = 50 
weights = {'broom': 5, 'rope': 8, 'torch': 5, 'coffin': 40, 'skeleton': 9, 'key1': 2, 'key2': 1, 'rock': 4, 'instructions': 0, 'mailbox': 60, 'letter': 1, 'gargoyle': 60, 'key3': 1, 'blockade': 60, 'oar': 5, 'boat': 30, 'button': 1, 'eagle nest': 60, 'faberge egg': 10, 'trophy case': 60, 'book': 3, 'facial': 1, 'gun': 3, 'safe': 60, 'diamondring': 5, 'junk': 1, 'combo': 1, 'egg': 1, 'flashlight': 3, 'oil': 4, 'bottle': 3, 'cork': 1, 'chandelier': 60, 'crown': 5, 'shovel': 5, 'knob': 3, 'twinkies': 2, 'pot of gold': 30, 'painting': 20, 'scanner': 60, 'toychest': 55} 
0

正如其他人所說,問題是,你使用的=代替:分配字典項目。

當您看到的錯誤消息,您可能沒有注意到,Python的是想確切地告訴你其中的錯誤是:

File "adventure.py", line 1 
    pickedup = {'coffin': '0', 'skeleton' = '0', 'rock': '0'} 
             ^
SyntaxError: invalid syntax 

如何看^在等號右邊指着簽名了嗎?

+0

我想感謝大家發現錯誤。我對Python相當陌生,正在學習,Stack Overflow一直是非常寶貴的資源。再次感謝! :) – Adam

0

python style guide建議保留72行字符<。就個人而言,我的目標是78.如果你這樣做,你的代碼更容易閱讀,作爲獎勵,它更容易發現問題。因此,舉例來說,如果你的文件是

secretpassageunearthed = 'No' 
pickedup = {'broom': '0', 'rope': '0', 'torch': '0', 'coffin': '0', 
'skeleton' = '0', 'key2': '0', 'rock': '0', 'key1': '0', 'mailbox': '0', 
'instructions': '0', 'letter': '0', 'key3': '0', 'oar': '0', 'boat': '0', 
'red button': '0', 'eaglenest': '0', 'faberge egg': '0', 'trophy case': '0', 
'facial': '0', 'gun': '0', 'safe': '0', 'diamond ring': '0', 'junk': '0', 
'combination': '0', 'egg': '0', 'flashlight': '0', 'cork': '0', 'bottle': '0', 
'oil': '0', 'crown': '0', 'shovel': '0', 'knob': '0', 'twinkies': '0', 
'scanner': '0', 'toychest': '0'} 

你會看到蟒蛇點正是在這個問題

[email protected] ~/tmp $ python3 test.py 
    File "test.py", line 3 
    'skeleton' = '0', 'key2': '0', 'rock': '0', 'key1': '0', 'mailbox': '0', 
      ^
SyntaxError: invalid syntax 

你基本上說:「我寫的代碼是難以閱讀,並不能找到錯誤」。