Python 错误:name";“蓝色”;未定义

Python 错误:name";“蓝色”;未定义,python,Python,当用户在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 numbe

当用户在
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()
好故事(顺便说一句)。不管怎么说,这是个问题。 调用代码中未定义的函数blue(),因为没有这样的函数)。如果你想调用blue1(),你会得到一个错误,那是因为 在使用函数之前,首先需要声明该函数 例如:

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

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

任何一种好的实践方法都是维护一个简单的代码结构,上面的所有函数和main是最后一个调用其他函数的函数

出现错误的原因是没有定义名为
blue()
的函数。在您的代码中是
blue1()
。我强烈建议投资一个短绒。

没有要求在大厅里使用蓝色功能?我不能复制这个。请注意,您的程序流非常糟糕。如果您在
red1
的某个地方调用例如
intro()
,则不会将程序倒带到顶部,因此在intro和之前发生的所有事情都不会执行。因此,在介绍之后,它将继续使用蓝色。调用函数不是GOTO语句。您应该以更好的方式进行设计(另外,避免递归,另请参见)好的,对于修改后的代码,错误很明显:您在创建函数之前就以这种方式调用它。Python自上而下地解释代码,因此调用上面稍后声明的函数将不起作用。你应该把你的主要游戏逻辑放在一个单独的函数中,你在代码的最后调用这个函数(例如,使用一个if主块)。请阅读,这不是什么。