【问题标题】:I keep getting a NameError in Python, saying that things are not defined, although I did define them我在 Python 中不断收到 NameError,说没有定义,尽管我确实定义了它们
【发布时间】:2023-03-12 20:53:02
【问题描述】:

所以我基于这个资源创建了这个,链接在这里:https://www.derekshidler.com/how-to-create-a-text-based-adventure-and-quiz-game-in-python/

编辑:这里是所有代码的 GitHub 链接:https://github.com/gabby-lucies/Quarter-1-Project/blob/master/intro.py

我目前卡住了,但是,在我的第一个选项中,无论何时选择下面列出的任何选项,我都会收到 NameError 说我的选项未定义。谁能给我任何建议?谢谢!另外,对不起,如果这是一个愚蠢的问题。

#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()

【问题讨论】:

  • 这是你的全部代码吗?我看不到在哪里定义了 option_petfwompoption_takefwompoption_eatfwomp
  • 仅供参考,您应该尽量避免使用全局变量(answer_A、answer_B 等),而是将它们传递给函数。
  • 可能不相关,但您可以通过将\n 放入字符串或使用三引号来编写多行字符串来一次打印多行:'''Your text here'''跨度>

标签: python nameerror undefined-function


【解决方案1】:

我已经查看了你的 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_animal的位置,option_petfwomp不存在,但是如果您删除所有这些函数调用并将它们移动到文件的末尾,它应该可以工作。

希望对你有帮助

【讨论】:

  • 感谢您的建议,但这不是我的问题的重点领域。给定第一组选项,option_smoke,option_creek,amd option_animal,无论我选择哪一个,它都说它没有定义。这也不是代码的全部。我觉得 250 行代码有点多余。
  • @GabrielleLucies-Yantis 我尝试运行您的代码,在运行 intro() 而不是 option_animal() 之后,第一个选择对我有用。你给它什么输入?
  • 您能否也将您的代码上传到 pastebin 并在此处分享链接,以便我可以看到整个内容。
  • @GabrielleLucies-Yantis 我已经根据您的 github 存储库编辑了我的答案
【解决方案2】:

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

What will you do?
A. Pet Fwomp B. Take Fwomp C. Eat Fwomp

如果我输入“A”并回车,它会运行option_petfwomp()

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

如果你添加:

def option_petfwomp():
    print("option_petfwomp")

def option_takefwomp():
    print("option_takefwomp")

def option_eatfwomp():
    print("option_eatfwomp")

你会明白它是如何工作的。基本上,您正在控制程序的流程。如果你定义了这些函数,你就可以从你的游戏中调用它们。

我的建议:

  1. 想想你想让你的代码做什么。
  2. 将其绘制为图表或伪代码。
  3. 编码。

您似乎复制粘贴了代码,却不知道它做了什么,也不知道您想要它做什么。这一切都很好,但是在希望其他人能够为您理解之前,请花点时间理解您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多