【发布时间】: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_petfwomp和option_takefwomp和option_eatfwomp。 -
仅供参考,您应该尽量避免使用全局变量(answer_A、answer_B 等),而是将它们传递给函数。
-
可能不相关,但您可以通过将
\n放入字符串或使用三引号来编写多行字符串来一次打印多行:'''Your text here'''跨度>
标签: python nameerror undefined-function