1

Edit: Looks like my main problem was, for whatever reason, having the boolean operator "or" in my choices would default the user to playing both choices, or the affirmative one instead of the negative one. If anyone can explain why, I would greatly appreciate it.

I am a novice to Python 3, that is probably obvious, so hopefully this is a simple fix. I really want to complete this game as it is one of the only projects I find as a "fun" way to learn that constantly keeps me focused (and frustrated). I would also eventually like to add more complicated features like a user inventory. I have been following some function-based youtube guides and essentially some paths in my game work (north>>west>>fight) while some do not (north>>east>>no). In the latter case, my program chooses "Y" every time on the third choice the player makes, even if the player writes "N". The issue pops up several other times in this chunk of code. Here, I'll show you(and if you see anything else fundamentally wrong with my code, let me know):

#Author: Joseph C 2021
# Cyoa (Kyoa) is a text-based fantasy game set in the realm of Kyoa. The Traveller awakens empty-handed and
# without any inkling of memory about who s/he is or what this strange medieval motor-punk world has to offer...
import random
import time

def playagain():
    answer = input("Do you want to play again? Y or N")
    if answer == "Y":
        print("OK.")
        chapter1()
    elif answer == "N":
        print("Goodbye.")

def Intro():
    print("Hey there...")
    time.sleep(.5)
    print("Hello?!")
    time.sleep(1.5)
    print("WAKE UP!")
    time.sleep(1)
    print("You awaken under a bodhi tree, to the annoying chirp of your Lilliputian fairy ally, Pyxys. She soon figures")
    print("that you've lost your memory due to the encounter up north, and respawned in the middle of a hilly field.")
    print("As a blank slate and denizen of the uncharted realm of Kyoa, you and Pyxys decide to explore once more.")

def chapter1():
    choice = input("Do you choose to go north, south, east, or west? \n")
    if choice == "north":
        print("You move forward in this direction. You witness a murder of winged orca whales in the blue, star"
              "covered yet daylit sky.")
        chapter1north()
    if choice == "east":
        chapter1east()
    if choice == "south":
        print("You walk a lot, pass by a lake with a distant background mountain range, and finally")
        print("come across an abandoned town. You loot a house and find a sack of gold! But suddenly,")
        print("a cohort of Vector Vipers and Glue Goblins gang up on you! You have died.")
        playagain()
    if choice == "west":
        print("You walk a lonely, boring path that twists and wanes but ultimately points west.")
        print("Before you know it, you stand before a tall evergreen forest, densely packed with")
        print("rocks and trees.")
        chapter2SF()

def chapter1north():
    time.sleep(.5)
    print("You continue walking north...")
    time.sleep(1)
    print("You come across a dense carniferous forest on the horizon to the east, and more spacious hillsides to"
          "the west.")
    choice = input("Will you go west toward the hilly plains, or east toward the dense woods? \n")
    if choice == "west":
        print("As you tread upon the hills towards the northwest, you encounter a spinfoam slime!")
        decision = input("Fight or flee?")
        if decision == "fight":
            print("You stomp on the paltry slime multiple times until it dissociates, bubbling into gas!"
                  "Congratulations! You gained 2 XP!")
            chapter2()
        if decision == "flee":
            print("You evade the paltry slime, yet you hear something from a distance...")
            time.sleep(1)
            print("The spinfoam slime sends a torrent of electricity towards you, burning you to a crisp!"
                  "You have died.")
            playagain()


    if choice == "east":
            print("You approach the thick forest, which you find is gated by barbed wire.")
            time.sleep(1) #at this point the code starts to bug. Maybe I need to make this in a new function
            InsectForestEntrance()

def InsectForestEntrance():
    decision2 = input("You decide to walk amid the perimeter of the barbed forest, heading in the"
                      "east and southeast directions...You manage to find an entrance."
                      " Will you enter? (Y or N)\n")
    if decision2 == "yes" or "Y":
        print("You enter the Insect Forest...")
        time.sleep(1)
        chapter1insectforest()
    elif decision2 == "no" or "N":
        print("You decide to travel south, as the forest expands throughout the entirety of the north.")
        chapter1east()
def chapter1insectforest():
    print("Immediately, the forest forks into two paths: one going northbound, the other southeastern.")
    decision2a = input("Which one will you take? north or southeast?")
    if decision2a == "north":
        pass
    if decision2a == "southeast":
        print("You go")
def chapter2():
    pass
def chapter3():
    pass
def chapter4():
    pass

Intro()
chapter1()
Joseph_C
  • 59
  • 4
  • 1
    Your `playagain` function doesn't do anything to stop the program if they say "no", so the code will just pick up where `playagain` was called. You'd need to add a call to `exit` or something similar after printing "good bye". Also, this style of ending every function with a call to another function is not a good practice to start. Most of your functions never return; they call more functions instead. Your code will eat up memory as it runs. You should return data from the functions (like what the user's response was), then have that returned data choose what functions are called later. – Carcigenicate Jan 17 '21 at 16:32
  • For the mean time, I think I will keep the foundation of the game function-based. Unless you can suggest something else to me. Anyway, your answer wasn't exactly a solution to my problem. I am still forced to enter the Insect Forest even when I say "no". – Joseph_C Jan 17 '21 at 16:50
  • 2
    A boolean expression like `decision2 == "yes" or "Y"` will always evaluate to `True`, see this [answer](https://stackoverflow.com/a/18491862/10987432). Also, `chapter1` can call `playagain`, which can call `chapter1`, which can call `playagain`... Do you see how this might be a problem? – Paul M. Jan 17 '21 at 16:59
  • I can't see the problem there. If you know how to fix my if's from running one after the other besides simply changing them to elif, let me know. – Joseph_C Jan 17 '21 at 19:11
  • Okay Paul, I don't see entirely if what you were trying to guide me to do was to omit the boolean operator, but what I did was omit the 'or' and make a single 'Y' response, then a single 'N' response. This appears to have fixed some of the issues. – Joseph_C Jan 17 '21 at 21:38
  • "besides simply changing them to elif" -- Do you want to avoid `elif` for some reason? If you only want to do one of a subset of things, that is the natural choice. That would help fix part of your problem. (The rest of it is mostly what Carcigenicate described.) – CrazyChucky Jan 17 '21 at 22:11
  • Your edited secondary question is answered [here](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true). – CrazyChucky Jan 17 '21 at 22:32
  • Thanks CrazyChucky. And I will do some digging as to how to make use of return instead of a lot of functions. – Joseph_C Jan 18 '21 at 15:45

0 Answers0