2017-04-15 39 views
0

我在做Excersice 35(分支和函數),但輸入的代碼沒有工作,經過一段時間尋找錯誤,我試圖從書中複製粘貼,但它沒有「科技工作Learn Python The Hard Way ex35

下面的代碼:

from sys import exit 

def gold_room(): 

    print "This room is full of gold. How much do you take?" 

    next = raw_input("> ") 
    if "0" in next or "1" in next: 
     how_much = int(next) 
    else: 
     dead("Man, learn to type a number.") 

    if how_much < 50: 
     print "Nice, you're not greedy, you win!" 
     exit(0) 
    else: 
     dead("You greedy bastard!") 


def bear_room(): 

    print "There is a bear 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 then 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 got no idea what that means." 


def cthulhu_room(): 

    print "Here you see the great evil Cthulhu." 
    print "He, it, whatever stares at you and you go insane." 
    print "Do you flee for your life or eat your head?" 

    next = raw_input("> ") 

    if "flee" in next: 
     start() 
    elif "head" in next: 
     dead("Well that was tasty!") 
    else: 
     cthulhu_room() 


def dead(why): 

    print why, "Good job!" 
    exit(0) 

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



start() 

當我運行它,它會出現‘>’,這是while循環,但是當我鍵入‘取蜜’,它說,死的ISN沒有定義,我不理解。 (對不起,如果我用英文寫錯了)

+0

您的發佈代碼中沒有顯示定義了「dead()」的位置 - 此函數在哪裏?它存在嗎? – Toby

+0

是的,它正好在'開始'上面 –

回答

1

縮進在python是相當重要的,它看起來像你沒有縮進while聲明,while下了整個事情,包括while本身應該def bear_room()下屬於。

bear_room應該是這樣的,否則你的start()調用之前運行while循環:

def bear_room(): 

    print "There is a bear 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 then 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 got no idea what that means." 
+0

OHHH哈哈我沒有看到,現在它正在工作,我不知道我怎麼沒有注意到,謝謝! –

0

只需將dead的函數定義移動到文件頂部(在它被調用的位置上方)。

from sys import exit 

def dead(why): 
    print why, "Good job!" 
    exit(0) 

def gold_room(): 

    print "This room is full of gold. How much do you take?" 

    next = raw_input("> ") 
    if "0" in next or "1" in next: 
     how_much = int(next) 
    else: 
     dead("Man, learn to type a number.") 

    if how_much < 50: 
     print "Nice, you're not greedy, you win!" 
     exit(0) 
    else: 
     dead("You greedy bastard!") 


def bear_room(): 

    print "There is a bear 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 then 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 got no idea what that means." 


def cthulhu_room(): 

    print "Here you see the great evil Cthulhu." 
    print "He, it, whatever stares at you and you go insane." 
    print "Do you flee for your life or eat your head?" 

    next = raw_input("> ") 

    if "flee" in next: 
     start() 
    elif "head" in next: 
     dead("Well that was tasty!") 
    else: 
     cthulhu_room() 




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


start() 
+0

這是一個有效的解決方案,但它並不依賴於這個問題的邏輯。操作系統聲稱「從書中複製粘貼」,這一定意味着書中也有「死」的字樣。另外,根據你的邏輯,你在'start'之前運行while循環,在這裏它解釋了遊戲的設置 – abccd

+0

啊,這是有道理的。 –

0

最有可能你在你的代碼有一個空白錯誤。這意味着你忘記了某個地方的標籤,因此你永遠不會達到dead(該功能)的定義。當它被調用時(dead("..."))它沒有被定義並且腳本停止。

這裏是你的代碼應該如何看起來像:

from sys import exit 

def gold_room(): 
    print "This room is full of gold. How much do you take?" 

    next = raw_input("> ") 
    if "0" in next or "1" in next: 
     how_much = int(next) 
    else: 
     dead("Man, learn to type a number.") 

    if how_much < 50: 
     print "Nice, you're not greedy, you win!" 
     exit(0) 
    else: 
     dead("You greedy bastard!") 

def bear_room(): 
    print "There is a bear 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 then 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 got no idea what that means." 

def cthulhu_room(): 
    print "Here you see the great evil Cthulhu." 
    print "He, it, whatever stares at you and you go insane." 
    print "Do you flee for your life or eat your head?" 

    next = raw_input("> ") 

    if "flee" in next: 
     start() 
    elif "head" in next: 
     dead("Well that was tasty!") 
    else: 
     cthulhu_room() 

def dead(why): 
    print why, "Good job!" 
    exit(0) 

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

start() 

真的要注意空格!

相關問題