2012-06-02 64 views
2

我很新很抱歉,如果我的問題不是真的具體或如果標題誤導,我一直在嘗試使用Python 2.7製作遊戲。到目前爲止,一切都很順利,但有一個問題,我得到一個語法錯誤,我不知道如何解決它。錯誤說: 「您的程序中有一個錯誤: *無法分配到文字(文本game.py,第28行)」 在這一行我試圖分配'n'的值爲1 ,下面的代碼:這段代碼有什麼問題? (Python,語法錯誤)

print """ 
--------------------------------------------- 
|           | 
|  TEXT GAME: THE BEGINNING!    | 
|           | 
--------------------------------------------- 
""" 
print "What is your name adventurer?" 
adv_name = raw_input("Please enter a name: ") 
print "Welcome " + adv_name + ", I am called Colosso. I am the great hero of the town Isern. \nWhile I was protecting the surrounding forests I was attacked and killed. \nNow I am nothing but a mere spirit stuck in the realm of Life. \nI am here because I swore to slay the corrupt great king Blupri. \nBlupri still lives therefore I cannot travel to the realm of Death. \nI need you to slay him and his minions so I may rest in peace" 
print """Do you want to help Colosso?: 
1.) Yes (Start playing) 
2.) No (Quit) 
""" 
dside = input("Please enter a number to decide: ") 
if dside == 2: 
    print "I knew you were a coward..." 
    raw_input('Press Enter to exit') 
elif dside == 1: 
    print "Great! Let's get this adventure started" 
    raw_input('Press Enter to continue') 
print """This is the tutorial level, here is where you will learn how to play. 
To move the letter of a direction, n is north, e is east, s is south, w is west. 
Press the '<' key to move up and the '>' key to move down. 
Try it! 
""" 
move = raw_input('Where would you like to go?: ') 
"n" = 1 
"e" = 2 
"s" = 3 
"w" = 4 
"<" = 5 
">" = 6 
if move == 1: 
    print "You move north." 
if move == 2: 
    print "You move east." 
if move == 3: 
    print "You move south." 
if move == 4: 
    print "You move west." 
print move 

我已經出了引號和單引號,但沒有工作 任何幫助或建議表示讚賞嘗試過。

+0

該錯誤意味着「literal」= value'正在完成。這是不允許的,並且比'42 = 0'更有意義。 42不是42而不是0.同樣,「文字」是「文字」而不是任何其他值。 – 2012-06-02 20:31:25

回答

1

Python中將「n」解釋爲一個字符串字面值,這意味着它本身就是一個值,您不能將值賦給另一個值。 =左側的標記需要是一個變量。

我建議刪除第28行中的n的雙引號。不是python編碼器,但這是我本能地做的。

+0

謝謝Rob!這是有效的,當我第一次移除它們時,我必須讀錯誤。這真是令人沮喪,謝謝你的幫助。我感謝它 – user1432812

+1

這是一種享受!回答有關我的新語言的問題很有趣。我現在把你交給Lev,他似乎比我有更多的Python體驗;-)我已經安裝了Python來玩你的代碼。所以謝謝你提出這個問題。 –

+1

@ user1432812考慮接受(最)有用答案,方法是檢查左側的勾號。通過這種方式,您可以指出問題已經解決,還可以獎勵發佈答案的人。雖然沒有急於,你可以稍後再做。 –

2

Rob is right,以及下面的代碼行沒有太大意義。

我建議就是:

move = raw_input('Where would you like to go?: ') 

if move == 'n': 
    print "You move north." 
elif move == 'e': 
    print "You move east." 
elif move == 's': 
    print "You move south." 
elif move == 'w': 
    print "You move west." 

或者,如果你真的想輸入到數字地圖出於某種原因,考慮創建一個字典:

directions = {"n": 1, "e": 2, "s": 3, "w": 4, "<": 5, ">": 6} 

然後,你可以這樣做:

if directions[move] == 1: 
    # etc 
+1

如果在第一個'if ...'之後使用'else if',那麼它會更快,所以如果所有先前的失敗都只檢查該條件。 – C0deH4cker

+0

@ C0deH4cker非常真實,我沒有注意到,而複製和粘貼。現在更新答案。 –

+0

Oh oh,elif。我一直在langs之間切換(c,cpp,objc,php,java),只有python使用elif。不過,我應該記住這一點,因爲python是我最好的語言。 – C0deH4cker