2015-11-07 107 views
-6

當用戶在hall()上選擇選項4時,它應該運行blue(),但是當我嘗試運行它時,出現錯誤,說明未定義blue()。我該如何解決?錯誤:未定義名稱「藍色」

import time 
import sys 

name = input ("Name: ") 

print ("Hello", (name), ", and welcome to my game that i made to learn python.") 
print ("How to play the game, just type the number its not that hard.") 
time.sleep(5) 

def intro(): 
    print ("You are in a room, to the south there is a torch on the wall and to the north there is a door.") 
    time.sleep(5) 
    print ("Your options are: ") 
    time.sleep(3) 
    print ("1. Do nothing") 
    print ("2. Go south and pick up the torch") 
    print ("3. Go north, open and go through the door") 
    print ("4. You decide to build an orphanage in the room, makes sense.") 

    choice = input(">>> ") 

    if choice == "1": 
     print("You decide to curl into a ball and go to sleep, you never wake up again. --GAME OVER--") 
     print("I guess you could try again if you must.") 
     time.sleep(5) 
     intro() 
    elif choice == "2": 
     print("You walk southwards towards the wall, grab the torch off the wall and hold it in your hands.") 
     print("You walk over towards the door and open it") 
     time.sleep(5) 
    elif choice == "3": 
     print("You walk over towards the door and open it, on the other side there is a dark corridor, you step forward and the door closes behind you. You get surrounded in darkness and die. --GAME OVER--") 
     print("I guess you could try again if you must.") 
     time.sleep(5) 
     intro() 
    elif choice == "4": 
     print("You can't build an orphanage in a room with nothing there idiot, you wasted your whole life attempting. --GAME OVER--") 
     print("I guess you could try again if you must.") 
     time.sleep(5) 
     intro() 
    else: 
     print("Type the correct number idiot") 
     intro() 

intro() 

def hall(): 
    print ("As you open the door a strong gust of cold air comes through making the torch flicker") 
    time.sleep(3) 
    print ("You continue up the corridor with the torch illuminating your surroundings, you feel like your being watched") 
    time.sleep(3) 
    print ("You keep walking for what seems like hours and you finally come across a part where the corridor splits off into 3 different ones") 
    print ("What do you do?") 
    print ("1. Go north.") 
    print ("2. Go east.") 
    print ("3. Go back the way you came.") 
    print ("4. Go west.") 
    time.sleep(5) 

hall() 

choice = input(">>> ") 

if choice == "1": 
     print("You head north but as soon as you do the ground under you crumbles and you fall. And die. --GAME OVER--") 
     print("I guess you could try again if you must.") 
     time.sleep(5) 
     hall() 
elif choice == "2": 
     print("You go down the east corridor and get a glimpse of a dark red light before it disappears.") 
     print("You continue to walk") 
     time.sleep(5) 
     red() 
elif choice == "3": 
     print("Well done, you just went back to the place you wanted to get out from, your legs are tired and your torch has gone out. idiot. --GAME OVER--") 
     print("I guess you could try again if you must.") 
     time.sleep(5) 
     hall() 
elif choice == "4": 
     print("You go down the west corridor and get a glimpse of a dark blue light before it disappears.") 
     print("You continue to walk") 
     time.sleep(5) 
     blue() 
else: 
    print("Type the correct number idiot") 
    time.sleep(5) 
    hall() 

def red1(): 
    print ("As you continue to walk down the corridor the air around you seems to head up more and more.") 
    time.sleep(3) 
    print ("You come around a little podium and on it is a piece of paper with the numbers 264 894 written on it") 
    time.sleep(3) 
    print ("You go to pick it up but it crumbles into dust and under the dust are the words blue carved into the podium") 
    time.sleep(3) 
    print ("you continue walking") 
    time.sleep(3) 
    print ("After a while you come across a shiny iron door with a keypad by the side on the keypad it spells the words red on it.") 
    time.sleep(3) 
    print ("You attempt to enter the correct code.") 

red1() 

code1 = input(">>> ") 

if code1 == "362 682": 
    print ("The door slides open without making a noise, you step into the door and continue walking") 
else: 
    print ("Incorrect code. Hint:RED") 
    print ("restarting...") 
    time.sleep(10) 
    intro() 

def blue1(): 
    print ("As you continue to walk down the corridor the air around you seems to get colder and colder more and more.") 
    time.sleep(3) 
    print ("You come around a little podium and on it is a piece of paper with the numbers 362 682 written on it") 
    time.sleep(3) 
    print ("You go to pick it up but it crumbles into dust and under the dust are the words red carved into the podium") 
    time.sleep(3) 
    print ("you continue walking") 
    time.sleep(3) 
    print ("After a while you come across a rusty iron door with a keypad by the side on the keypad it spells the words red on it.") 
    time.sleep(3) 
    print ("You attempt to enter the correct code.") 

blue1() 

code2 = input(">>> ") 

if code2 == "264 894": 
    print ("The door slides open without making a noise, you step into the door and continue walking") 
else: 
    print ("Incorrect code. Hint:BLUE") 
    print ("restarting...") 
    time.sleep(10) 
    intro() 
+0

廳內功能有沒有藍色呼叫? – yeg

+0

我無法複製此內容。請注意,您的程序流程非常糟糕。如果你打電話給'intro()'在'red1'的某個地方,那麼這將*不會*將你的程序倒回到頂部,所以發生在前面和那個點之間的所有內容都不會被執行。因此,在介紹之後,它會繼續藍色。調用一個函數不是一個GOTO語句。你應該更好地設計這個(另外,避免遞歸,另請參見[這個答案])(http://stackoverflow.com/a/23294659/216074)) – poke

+1

好吧,用你修改的代碼,錯誤是非常清楚的:你在創建之前調用函數的方式。 Python從上到下解釋代碼,因此調用稍後聲明的函數將不起作用。您應該將您的主遊戲邏輯放入您在代碼最後調用的獨立函數中(例如,使用if-main模塊)。 – poke

回答

1

不錯的故事btw)。無論如何,這個問題。 你調用函數blue(),它沒有在你的代碼中定義 - 沒有這樣的函數)。如果你的意思是叫藍天航空公司(),你會得到一個錯誤,那是因爲 首先需要使用前聲明函數它 例如:

1.this will not work: 
blue() 
def blue(): ... 

2. this will work: 
def blue(): ... 
blue() 

任何方式很好的做法其良好的保持簡單的代碼結構,上面的所有功能和主要是最後一個調用其他功能。

0

您收到錯誤的原因是因爲確實沒有定義名爲blue()的函數。這是你的代碼中的blue1()。我強烈建議投資棉絨。