我一直在Python中遇到一个命名错误,说东西没有定义,尽管我定义了它们

我一直在Python中遇到一个命名错误,说东西没有定义,尽管我定义了它们,python,nameerror,undefined-function,Python,Nameerror,Undefined Function,因此,我正在基于此资源创建此链接: 编辑:以下是指向所有代码的GitHub链接: 然而,我现在陷入了困境,在我的第一个选项中,任何时候选择下面列出的选项,我都会收到一个名称错误,说我的选项没有定义。有人能给我一些建议吗?谢谢另外,如果这是一个愚蠢的问题,我也很抱歉 #importing import time #How players could respond answer_A = ["A", "a"] answer_B = ["B", "b"] answer_C = ["C", "c"]

因此,我正在基于此资源创建此链接:

编辑:以下是指向所有代码的GitHub链接:

然而,我现在陷入了困境,在我的第一个选项中,任何时候选择下面列出的选项,我都会收到一个名称错误,说我的选项没有定义。有人能给我一些建议吗?谢谢另外,如果这是一个愚蠢的问题,我也很抱歉

#importing
import time

#How players could respond
answer_A = ["A", "a"]
answer_B = ["B", "b"]
answer_C = ["C", "c"]
yes = ["Y", "y", "yes"]
no = ["N", "n", "no"]

#Objects
fwomp = 0
bownarrow = 0
sword = 0
money = 0

#playerName = input("Enter Your Name: ") #gets the player's name, obviously
required = ("\nUse only A, B, or C.\n")

#Startup
def intro():
    #print("There is no saving. Sorry!")
    #print("Hello! Please enter your name.")
    #print("Nice to meet you!")
    print("Let's begin!")
    print("You wake up in a forest. You can't remember anything. You look around and find")
    print("a small creek. You hear sound nearby that sounds like some sort of woodland creature.")
    print("You also see some smoke in the distance. What would you like to do ?")
    time.sleep(2)
    #Choice 1 Options
    print("A. Follow the creek.")
    print("B. Follow the sound of the woodland creature.")
    print("C. Walk towards the smoke.")
    choice = input(">>> ") #gets choice
    time.sleep(1.5)
    if choice in answer_A:
        option_creek() #Gives player creek option
    elif choice in answer_B:
        option_animal() #Give Fwomp options
    elif choice in answer_C:
        option_smoke() #Gives smoke options
    else:
        print(required)
        intro()

def option_creek():
    print("You follow the creek for an hour, and you eventually come across the river.")
    print("You followed the river for another hour, and you found a riverside village.")
    print("You walk into the village, desperately looking for food and water.")
    print("You come across a large pub. It doesn't look very busy.")
    print("You also come across an elegant resturant. It must be very expensive.")
    print("There is also family outside of their house eating freshly picked food from their garden.")
    print("Where will you go?")
    print("A. Pub B. Resturant C. Family")
    time.sleep(2.5)
    choice = input(">>> ")
    if choice in answer_A:
        option_pub()
    if choice in answer_B:
        option_resturant()
    if choice in answer_C:
        option_family()
    else:
        print(required)
        option_creek()

def option_smoke():
    print("You walk towards the smoke. Eventually you find the source of the smoke.")
    print("The smoke is coming from a lost and very angry tribe.")
    print("They also don't speak your language.")
    print("When they spot you, and you have nothing to offer them in return, so they assume you are there to kill them.")
    print("Anways, fifty of them shot you with arrows all at once.")
    time.sleep(2)
    print("Also, you're dead!")
    time.sleep(2)
    option_smoke()


def option_animal():
    print("Seriously, you walked towards a strange animal sound just like that?")
    print("Luckily, it is only a Fwomp. It doesn't kill you, but it does glare at you strangely.")
    print("Fwomps are cute, so you want to pet it.")
    print("You also want to take the Fwomp.")
    print("You're also hungry and horrible so you also kind of want to eat the Fwomp.")
    print("What will you do?")
    print("A. Pet Fwomp B. Take Fwomp C. Eat Fwomp") 
    time.sleep(2.5)
    choice = input(">>> ")
    if choice in answer_A:
        fwomp = 1
        option_petfwomp()
    elif choice in answer_B:
        fwomp = 1
        option_takefwomp()
    elif choice in answer_C:
        option_eatfwomp()
    else:
        print(required)
option_animal()

我已经查看了你的github代码,所以我正在修改我的答案

在每个选项函数之后,您将立即调用它,如图所示

def option_animal():
    print("Seriously, you walked towards a strange animal sound just like that?")
    print("Luckily, it is only a Fwomp. It doesn't kill you, but it does glare at you strangely.")
    print("Fwomps are cute, so you want to pet it.")
    print("You also want to take the Fwomp.")
    print("You're also hungry and horrible so you also kind of want to eat the Fwomp.")
    print("What will you do?")
    print("A. Pet Fwomp B. Take Fwomp C. Eat Fwomp") 
    time.sleep(2.5)
    choice = input(">>> ")
    if choice in answer_A:
        fwomp = 1
        option_petfwomp()
    elif choice in answer_B:
        fwomp = 1
        option_takefwomp()
    elif choice in answer_C:
        option_eatfwomp()
    else:
        print(required)
option_animal()
由于python是一种过程语言,因此只有文件中此调用之前的代码存在,因此,在调用
option\u animal
的地方,
option\u petfwomp
不存在,但是如果删除所有这些函数调用并将它们移动到文件末尾,应该可以工作


希望这有帮助

我已经查看了您的github代码,所以我正在更改我的答案

在每个选项函数之后,您将立即调用它,如图所示

def option_animal():
    print("Seriously, you walked towards a strange animal sound just like that?")
    print("Luckily, it is only a Fwomp. It doesn't kill you, but it does glare at you strangely.")
    print("Fwomps are cute, so you want to pet it.")
    print("You also want to take the Fwomp.")
    print("You're also hungry and horrible so you also kind of want to eat the Fwomp.")
    print("What will you do?")
    print("A. Pet Fwomp B. Take Fwomp C. Eat Fwomp") 
    time.sleep(2.5)
    choice = input(">>> ")
    if choice in answer_A:
        fwomp = 1
        option_petfwomp()
    elif choice in answer_B:
        fwomp = 1
        option_takefwomp()
    elif choice in answer_C:
        option_eatfwomp()
    else:
        print(required)
option_animal()
由于python是一种过程语言,因此只有文件中此调用之前的代码存在,因此,在调用
option\u animal
的地方,
option\u petfwomp
不存在,但是如果删除所有这些函数调用并将它们移动到文件末尾,应该可以工作


希望这有帮助

您的代码没有错,只是不完整。我假设您正在使用它来学习Python。让我问你一个简单的问题。当你按run时,你会得到一些选项

What will you do?
A. Pet Fwomp B. Take Fwomp C. Eat Fwomp
如果我键入“A”并按enter键,它将运行
option\u petfwomp()

但是在您的代码中没有定义这个函数

如果您添加:

def option_petfwomp():
    print("option_petfwomp")

def option_takefwomp():
    print("option_takefwomp")

def option_eatfwomp():
    print("option_eatfwomp")
你会明白它是如何工作的。基本上,您是在控制程序的流程。如果你定义了这些函数,你可以在游戏中调用它们

我的忠告是:

  • 想想你希望你的代码做什么
  • 将其映射为图表或伪代码
  • 编码

  • 似乎您复制粘贴了代码,而不知道它做什么,也不知道您希望它做什么。这很好,但是在希望其他人理解你的代码之前,先花点时间理解你的代码。

    你的代码没有错,只是不完整。我假设您正在使用它来学习Python。让我问你一个简单的问题。当你按run时,你会得到一些选项

    What will you do?
    A. Pet Fwomp B. Take Fwomp C. Eat Fwomp
    
    如果我键入“A”并按enter键,它将运行
    option\u petfwomp()

    但是在您的代码中没有定义这个函数

    如果您添加:

    def option_petfwomp():
        print("option_petfwomp")
    
    def option_takefwomp():
        print("option_takefwomp")
    
    def option_eatfwomp():
        print("option_eatfwomp")
    
    你会明白它是如何工作的。基本上,您是在控制程序的流程。如果你定义了这些函数,你可以在游戏中调用它们

    我的忠告是:

  • 想想你希望你的代码做什么
  • 将其映射为图表或伪代码
  • 编码

  • 似乎您复制粘贴了代码,而不知道它做什么,也不知道您希望它做什么。这很好,但是在希望其他人能理解你的代码之前,先花点时间理解你的代码。

    这是你的全部代码吗?我看不出
    option\u petfwomp
    option\u takefwomp
    option\u eatfwomp
    是在哪里定义的。仅供参考,您应该尽量避免使用全局变量(answer\u A、answer\u B等),而是将它们传递给函数。可能不相关,但是您可以通过在字符串中放入
    \n
    或使用三重引号来编写多行字符串,一次打印多行:“此处的文本”这是您的全部代码吗?我看不出
    option\u petfwomp
    option\u takefwomp
    option\u eatfwomp
    是在哪里定义的。仅供参考,您应该尽量避免使用全局变量(answer\u A、answer\u B等),而是将它们传递给函数。可能不相关,但是,您可以通过在字符串中放入
    \n
    或使用三重引号来编写多行字符串,一次打印多行:“此处的文本”谢谢您的建议,但这不是我当前问题的重点。当我给出第一组选项,选项烟,选项溪,amd选项动物,无论我选择哪一个,它说它没有定义。这也不是全部代码。我觉得250行代码有点太多了。@GabrielleLucies Yantis我试过运行你的代码,第一个选择在运行
    intro()
    而不是
    option\u animal()
    后对我有效。您提供了哪些信息?您是否可以将您的代码上传到粘贴箱并在此处共享链接,以便我可以看到整个内容。@GabrielleLucies Yantis我根据您的github Repository编辑了我的答案谢谢您的建议,但这不是我当前问题的重点。当我给出第一组选项,选项烟,选项溪,amd选项动物,无论我选择哪一个,它说它没有定义。这也不是全部代码。我觉得250行代码有点太多了。@GabrielleLucies Yantis我试过运行你的代码,第一个选择在运行
    intro()
    而不是
    option\u animal()
    后对我有效。你给它什么输入?你能把你的代码上传到一个粘贴箱并在这里分享链接吗?这样我就可以看到整个事情了。@Gabrielleucies Yantis我根据你的github数据库编辑了我的答案