2014-05-19 67 views
2

這是對簡單文本冒險的嘗試。我無法設法使條件按我希望的方式工作。當提供用戶輸入時,例如被詢問時的「左」,它告訴我「左」沒有被定義,即使我將它列在那裏。這是代碼。條件在我的Python代碼中不起作用

from time import sleep 

name = raw_input("What is your name? ") 
live = raw_input("Where do you live? ") 

print "%s. You have just come to your senses." % (name) 
print "You are in %s, lying in the road." % (live) 
sleep (5) 

from datetime import datetime 
now = datetime.now() 

print "You look at your watch. There's a crack in the screen." 
sleep (5) 
print "Surely it cannot be %s/%s/%s at %s o clock..." % \ 
(now.day, now.month, now.year, now.hour) 
sleep (5) 
print "There is light blotting orange onto your closed eyes." 
sleep (7) 
print "You open your eyes." 
sleep (5) 
print "you see the road. Scarred, cracked and peppered with dead birds." 
sleep (5) 
print "Smoke thins out over the horizon." 
sleep (5) 
print "There is a house you are familiar with, to your right." 
sleep (3) 
print "There is a road leading uphill to your left." 
sleep (3) 
print "Where do you turn to?" 

answer = raw_input("Type left or right and press Enter.") 
if answer == left: 
    print "The road winds uphill." 
sleep (5) 
print "Glass crunches underfoot." 
sleep (5) 
print "The trees are all bare." 
sleep (5) 
print "The leaves turned to dust." 
print "They are ground into the cracks, between the paving stones." 
sleep (5) 
if answer == right: 
    print "The corridor turns sharply." 
sleep (2) 
print "You go deeper inside until you can't see anything" 
sleep(5) 
print "You hear a noise from the floor below." 
sleep (5) 
print "You can just make out a white doorframe under the staircase." 
sleep (5) 
answer2 = raw_input("Do you go into the basement? Type yes, or no.") 
if answer2 == yes: 
    print "The stairs creak. You fumble along the wall for a lightswitch" 
sleep (5) 
print "Do you press it?" 
if answer3 == yes: 
    print "Light floods the basement. It leaves you blinded for a few seconds..." 
sleep (5) 
print "Were you pushed or did you fall?" 
sleep (2) 
print "No one remembers that day, or any other. The End" 
if answer2 == no: 
    print "You can't see a thing. You decide to get out back to the street." 
sleep (5) 
print "Night is falling. You are hopelessly lost." 
sleep (20) 
sys.exit 
+1

如果'left'應該是一個字符串,爲什麼不用'回答==「左」吧? –

+0

當我這樣做,它說:文件「介紹。PY」的第33行 如果答案==‘左’ ^ 語法錯誤:無效的語法 – BenjaminJB

+0

恐怕我剛剛開始今天的Python,所以我不是最有經驗的 – BenjaminJB

回答

0
answer = raw_input("Type left or right and press Enter.") 

如果答案== 「左」

 print "The road winds uphill." 
sleep (5) 
print "Glass crunches underfoot." 
sleep (5) 
print "The trees are all bare." 
sleep (5) 
print "The leaves turned to dust." 
print "They are ground into the cracks, between the paving stones." 
sleep (5) 

如果答案== 「右」

 print "The corridor turns sharply." 
sleep (2) 
print "You go deeper inside until you can't see anything" 
sleep(5) 
print "You hear a noise from the floor below." 
sleep (5) 
print "You can just make out a white doorframe under the staircase." 
sleep (5) 
answer2 = raw_input("Do you go into the basement? Type yes, or no.") 

如果ANSWER2 == 「是」

 print "The stairs creak. You fumble along the wall for a lightswitch" 
sleep (5) 
print "Do you press it?" 

如果ANSWER3 == 「是」

 print "Light floods the basement. It leaves you blinded for a few seconds..." 
sleep (5) 
print "Were you pushed or did you fall?" 
sleep (2) 
print "No one remembers that day, or any other. The End" 

如果ANSWER2 == 「不」

 print "You can't see a thing. You decide to get out back to the street." 
sleep (5) 
print "Night is falling. You are hopelessly lost." 
sleep (20) 
sys.exit 

您需要報價,因爲否則公司mpiler假定它是一個變量,你想要的常量值是「左」,「是」等等,不是一個叫左的變量,是的,等等

+0

非常感謝你,出。現在來處理遊戲本身的問題...... – BenjaminJB

6
if answer == "left": 

你需要用繩子

if answer == left: 

試圖比較變量左邊,這蟒蛇是正確的告訴你變量的答案比較字符串沒有變左命名

粗略的另一種可能性是定義一個名爲left的變量在頂部

left = "left" 

(請記住,這同樣適用於你的其他比較)

2

它看起來像你還沒有定義的變量leftrightyes也不no。由於輸入是一個字符串,你應該把它比對的字符串:

if answer == "left": 
    ... 
if answer == "right": 
    ... 
if answer == "yes": 
    ... 
if answer == "no": 
    ... 

注: Python中的字符串由單引號'some string'或雙引號括"another string",或者像@JoranBeasley提到,三引號"""some multi-line string"""

+1

甚至三重引號'「」「一些字符串」「」「'+1都是一樣的 –

0

你的問題很可能會與此解決:

if answer == "left":