函数未定义错误,尽管它是

时间:2020-05-21 15:52:57

标签: python python-3.x

大约3天前,我开始学习Python作为一种业余爱好。 今天,我想尝试用Python编写文本冒险程序。

当我尝试测试代码时,我遇到了一个错误,但是我不知道为什么。有人可以帮我吗?

代码:

name = 'bot'
def intro():
    print("On day you wake up but you don't remember anything, except your name.\n"
          "You are in a unknown room. Right in front of you is a unlocked Computer.\n"
          "\nWhat do you do?\n"       
          "\n1) Go to the computer.\n"
          "2) Try to escape.\n"
          "3) ")

    anwser = input(">>> ")

    if anwser.lower() == "1" or "go to the computer":
        return computer()
    elif anwser.lower() == "2" or "try to escape":
        return escape()

intro()

def computer():
    print("You go to the computer and there is a folder called" +name)
    print("\nWhat do you do:\n"
          ""
          "\n1) Open the folder.\n"
          "2) Delete the folder\n"
          "3) Lock the computer and try to escape.")
    anwser = input(">>> ")

    if anwser.lower() == "1" or "open the folder":
        print("You open the folder an there a lot of dokuments.\n But you see a folder called \" Project Raspbrain \"")
    elif anwser.lower() == "2" or "delete the folder":
        print("You decide to delete the folder but you feel wired and fall on the ground.\n"
              "You die!")
    elif anwser.lower() == "3" or "lock the computer and try to escape":
        escape()

def escape():
    print("You see a door on your right side, you decide to go through it but there are two guards in front of you\n"
          "What do you do?\n"
          "\n1) Kill the guards"
          "\n 2) Run")
    anwser = input(">>>")

    if anwser == "Kill the guards" or "1":
        print("You try to kill the guards but don't have any weapons.\n Instead they kill you.\n You die")
    elif anwser == "2" or "run":
        print("You try to run but you stumble an fall on your head.\n You die!")

2 个答案:

答案 0 :(得分:1)

您需要将intro()移动到该脚本的末尾,并且您的第一行name = bot的连线非常紧密,我认为bot是一个字符串,并且代码如下所示。该代码将运行良好。

请记住,python是脚本语言,它将按代码顺序运行,因此在您的方案中,您在intro()定义之前运行computer(),它将弹出未定义错误。

name = 'bot'
def intro():
    print("On day you wake up but you don't remember anything, except your name.\n"
          "You are in a unknown room. Right in front of you is a unlocked Computer.\n"

          "\nWhat do you do?\n"

          "\n1) Go to the computer.\n"
          "2) Try to escape.\n"
          "3) ")

    anwser = input(">>> ")

    if anwser.lower() == "1" or "go to the computer":
        return computer()
    elif anwser.lower() == "2" or "try to escape":
        return escape()




def computer():
    print("You go to the computer and there is a folder called" +name)
    print("\nWhat do you do:\n"
          ""
          "\n1) Open the folder.\n"
          "2) Delete the folder\n"
          "3) Lock the computer and try to escape.")
    anwser = input(">>> ")

    if anwser.lower() == "1" or "open the folder":
        print("You open the folder an there a lot of dokuments.\n But you see a folder called \" Project Raspbrain \"")
    elif anwser.lower() == "2" or "delete the folder":
        print("You decide to delete the folder but you feel wired and fall on the ground.\n"
              "You die!")
    elif anwser.lower() == "3" or "lock the computer and try to escape":
        escape()

def escape():
    print("You see a door on your right side, you decide to go through it but there are two guards in front of you\n"
          "What do you do?\n"
          "\n1) Kill the guards"
          "\n 2) Run")
    anwser = input(">>>")

    if anwser == "Kill the guards" or "1":
        print("You try to kill the guards but don't have any weapons.\n Instead they kill you.\n You die")
    elif anwser == "2" or "run":
        print("You try to run but you stumble an fall on your head.\n You die!")


intro()

答案 1 :(得分:1)

请说明什么是“机器人”?是预定义变量还是语法错误?

对于函数错误,只需将介绍函数调用移到底部

def intro():
    (…)

def computer():
    (…)

def escape():
    (…)

intro()