所以我是一個絕對的初學者,通過Zed Shaw的Learn Python The Hard Way進行工作。 由於某些原因,當我運行一個程序時,我隨機獲得了不同的輸出。下面是我的代碼的一部分以及一些不一致的輸入/輸出。我已經連續多次嘗試過這種方式,有時代碼會正常工作,並調用下一個函數,有時會跳過大部分代碼。在不同輸出的終端上運行相同的程序
這裏是我的代碼不能始終如一地運行......
def bear_room():
print "There is a bear in here."
print " The bear has a bunch of honey."
print " The fat bear is in front of another door."
print " How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input(">")
if next == "take honey":
dead("The bear looks at you and slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print " I have no idea what that means."
下面是一些不一致的輸出... 這裏我運行該程序,並使用「左」輸入的提示。
Theresa-Beckers-MacBook-Pro:Summer 2013 Python leafgirl12$ python ex35.py
You are in a dark room.
There is a door to your right and left
Which one do you take?
>left
You stumble around the room until you starve. Good job!
在這裏,我立即做了同樣的事情,這次它運行,但輸出是不同的。
Theresa-Beckers-MacBook-Pro:Summer 2013 Python leafgirl12$ python ex35.py
You are in a dark room.
There is a door to your right and left
Which one do you take?
>left
There is a bear in here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
我知道在C++中創建新的變量時,它可以堆棧VS堆的問題,但我續找到在同一臺計算機上的Python功能的任何答案。我還重新輸入了我的代碼,以防出現一些我看不到的縮進錯誤。有幾次,當我繼續輸入「take honey」時,我能夠得到正確的輸出結果,但這隻能用一半的時間,而「嘲諷的熊」還沒有起作用。它只是直接傳遞給其他人。 有什麼想法?這有道理嗎?
您已在此發佈錯誤代碼。它不是一貫運行的'bear_room'代碼;它是不一致運行的'start'代碼。 – abarnert
當你輸入「left」時,你是否意外地在之前或之後放置了空白區域? – cmd
調試這個問題的最好方法是添加類似'print'的東西你在每個'raw_input()'語句後面說'{!r}''format(next)',所以你可以看到_exactly_你輸入的內容。 – abarnert