在python中定义函数 - 代码块不会运行

时间:2017-01-07 15:30:59

标签: python function

我在使用python中的函数时遇到了很多麻烦。我是一个初学者,所以我正在创建像游戏这样的迷宫,并希望使用功能,以便用户可以返回以前的位置,如果他们选择。问题是“def sec2():”之后的所有代码都没有运行,它完全跳过这段代码。因此,如果用户要运行程序并选择左侧,程序将以“啊,没关系,记住这些数字”结束,从不提示用户提供任何内容或从sec2打印任何内容。我相信我的问题可能会随着缩进而发生。如果有人知道为什么我的函数下的代码没有执行,请告诉我!非常感谢!

print ("********")
print ("Hey there soldier. This is General Chris.")
print ("I understand you are in quite the situation!")
print ("Just 4 hours ago, your patrol was ambushed by ISIS...You may not rememeber much after being knocked unconscious!")
name = input('Whats your name, soldier?')
print ("********")
print ("Alright, here's the deal",name)
print ("You are now being held hostage in  an encampment near Soran, Iraq.")
print ("Unfortunately for you, our hackers have recieved intel that your captors plan on executing you in just two hours.")
print ("You dont have long to make your escape! Get moving fast!")

def sec1():
    print ("********")
    print ("You have two doors in front of you. Do you choose the door on the left or right?")
    room1 = input('Type L or R and hit Enter.')

    if room1 == "L":
        print ("********")
        print ("Good choice",name)
        print ("Whats this? A slip of paper says '8642' on it...Could it mean something?")
        print ("Ah, nevermind! Remember those numbers!")

        def sec2():
            print ("********")
            print ("Now you have a choice between crawling into the cieling, or using the door!")
            room2 = input('Type C for cieling or D for door, and hit Enter!')

            if room2 == "C":
                print ("********")
                print ("Sure is dark up here in the ducts!")
                print ("Stay quiet, and move very slowly.")

                def ductoptionroom():
                    print ("Do you want to continue into the ducts or retreat?")
                    ductoption = input('Type C or R and hit Enter!')

                    if ductoption == "C":
                        print ("You are continuing into the ducts!")

                    elif ductoption == "R":
                        sec2()

                    else:
                        print ("Focus on the mission!")



            elif room2 == "D":
                print ("********")
                print ("You slowly turn the door knob and see a guard standing there...He doesnt notice you!")
                print ("Look, theres a piece of rope on the ground! You could use this to strangle the guard!")

                def guardkillroom():
                    print ("Do you want to try and kill the guard so you can continue on your escape?")
                    guardkill = input ('Type K for kill or R for retreat and hit Enter!')

                    if guardkill == "K":
                        print ("********")
                        print ("You sneak behind the unsuspecting guard and quickly pull the rope over his neck!")
                        print ("You've strangled the guard! Now you can continue on your mission!")

                    elif guardkill == "R":
                        guardkillroom()

                    else:
                        print ("We dont have all day!")
                        guardkillroom()

            else:
                print ("Focus soldier!")
                room2()



    elif room1 == "R":
        print ("********")
        print ("Uh oh. Two guards are in this room. This seems dangerous.")
        print ("Do you want to retreat or coninue?")
        roomr = input('Type R or C and hit enter.')

        if roomr == "R":
            print ("********")
            print ("Good choice!")
            sec1()

        elif roomr == "C":
            print ("********")
            print ("You continue and are spotted by a guard.")
            print ("***MIISSION FAILED***")

        def gameover1():
            print ("Do you want to retry?")
            retry1 = input("Type Y or N and hit enter!")

            if retry1 == "Y":
                sec1()

            elif retry1 == "N":
                print ("********")
                print ("Thanks for playing!")

            else:
                gameover1()
sec1()

1 个答案:

答案 0 :(得分:1)

看起来你只定义for /F "tokens=1,2 delims==" %%a in ("!search!=!replaceVal!") do set replace=!replace:%%a=%%b! 而你实际上从未调用它。当你执行类似sec2之类的操作时,它只会告诉python在调用该函数时应该发生什么。在您从代码中的其他位置运行def myfunc()之前,代码将永远不会运行。唯一的地方myfunc()sec2内递归的(如果玩家决定退出管道)

因此要使用sec2,您需要在sec2内的其他位置调用它 我不确定应该在快速阅读游戏的基础上进行测试,但是为了测试你可以做类似下面的事情

sec1

此外,由于 print ("Ah, nevermind! Remember those numbers!") def sec2(): .... elif room2 == "D": sec2() 定义在sec2内,因此sec1也只能在sec2内调用sec1。我怀疑这不是预期的(虽然我可能是错的)。要解决此问题,您可以执行以下操作

def sec2():
    ...
def sec1():
    ... #All the same code as before except for the sec2 definition

sec1()