簡體   English   中英

在終端以不同的輸出運行相同的程序

[英]Running same program in terminal with different output

所以我是一個絕對的初學者,並且通過Zed Shaw的《學習Python的艱難方法》進行研究。 今天由於某種原因,當我運行程序時,我會隨機獲得不同的輸出。 以下是我的代碼的一部分以及一些不一致的輸入/輸出。 我已經連續嘗試了多次,有時它的代碼可以正常工作並調用下一個函數,有時它會跳過大部分功能。

這是我的代碼運行不一致...

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."

這是一些不一致的輸出...在這里,我運行程序並在提示符下使用輸入“ left”。

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 ++中創建新變量時,它可能是堆棧還是堆的問題,但是我在同一台計算機上找不到Python函數的任何答案。 我還重新輸入了代碼,以防萬一我沒有看到縮進錯誤。 幾次,當我繼續並輸入“ take honey”時,我已經能夠獲得正確的輸出,但這只能工作一半時間,而“嘲諷熊”卻根本無法工作。 它只是直接傳遞到其他。 有什么想法嗎? 這有意義嗎?

通過查看此練習的代碼 ,您肯定在其中一種嘗試中拼寫了“ left”,請注意,這可能只是不必要的大寫字母,也可能是開頭或結尾的意外空格。

這是有問題的代碼:

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

如果您完全輸入“ left”並按Enter,則應始終進入熊室。

在“左”或“右”后跟隨空白將使您餓死。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM