0

So I'm creating this based of this resource, linked here: https://www.derekshidler.com/how-to-create-a-text-based-adventure-and-quiz-game-in-python/

EDIT: Here is a GitHub link to all the code: https://github.com/gabby-lucies/Quarter-1-Project/blob/master/intro.py

I'm currently stuck however, in my very first option, anytime to choose any of the options listed below, I get a NameError saying that my options are not defined. Can anyone give me any advice? Thanks! Also, sorry if this is a dumb question.

#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()
  • Is this your entire code? I don't see where `option_petfwomp` and `option_takefwomp` and `option_eatfwomp` are defined. – Kevin Oct 04 '19 at 13:22
  • 1
    Just fyi, you should try to refrain from using global variables (answer_A, answer_B etc.) and instead pass them along to the functions. – Nathan Oct 04 '19 at 13:24
  • Perhaps unrelated, but you can print multiple rows at once by either putting `\n` in your string or using triple quotations to write multi-line strings: '''Your text here''' – Dorijan Cirkveni Oct 04 '19 at 13:29

2 Answers2

2

I've looked over your github code, so i'm changing my answer for that

After each of your option functions, you're immediately calling it, shown here

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

As python is a procedural language, only the code that is before this call in the file exists, thus, at the point where you call option_animal, option_petfwomp doesn't exist, however if you remove all of these function calls and move them to the end of the file, it should work.

Hope this helps

Harvey
  • 114
  • 1
  • 8
  • Thank for your advice, but that is currently not the focus area of my issue. Whem given the 1st set of options, option_smoke, option_creek, amd option_animal, no matter which one I choose, it says it is not defined. This isn't the entirety of the code either as well. I figured 250 lines of code was a bit excessive. – Gabrielle Lucies-Yantis Oct 07 '19 at 00:43
  • @GabrielleLucies-Yantis I tried running your code and the first choice worked for me after running `intro()` instead of `option_animal()`. What inputs are you giving it? – Harvey Oct 07 '19 at 07:56
  • Could you also upload your code to a pastebin and share the link here so that I could see the whole thing. – Harvey Oct 07 '19 at 09:00
  • @GabrielleLucies-Yantis I've edited my answer based off of your github repositry – Harvey Oct 16 '19 at 08:27
0

Your code is not wrong, it's simply incomplete. I'm assuming you're using this to learn Python. Let me ask you a quick question. When you press run, you get some options.

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

If I type "A" and hit enter, it runs option_petfwomp()

But in your code you haven't defined this function.

If you add:

def option_petfwomp():
    print("option_petfwomp")

def option_takefwomp():
    print("option_takefwomp")

def option_eatfwomp():
    print("option_eatfwomp")

you'll understand how it works. Basically, you are controlling the flow of the program. If you define these functions, you can then call them from your game.

My advise:

  1. Think about what you want your code to do.
  2. Map it out as a diagram or psuedo-code.
  3. Code it.

It seems like you copy-pasted the code, without knowing what it does, or knowing what you want it to do. That's all fine, but take time out to understand your code before hoping someone else will understand it for you.

DUDANF
  • 2,618
  • 1
  • 12
  • 42