0

I'm struggling with my project for university. I'm doing a text-based adventure game that is rather liner but i can't seem to get past this block.

I've tried calling the list to show "You have chosen..." but i can't seem to get it to do that, I've tried following what i did previously with the other list but i think im lost.

import random
import time

list_intro = ["The drawbridge.", "The sewers."]  # naming the 2 indexes to paths ingame.
hall_rooms = ["Left door", "Right door"]


def intro():
    print("You heard at the local tavern of a abandoned castle holding many treasures.")
    print("You journey for 7 days until you find yourself at the bottom of the path leading to the castle.")
    print("Standing at the foot of the castles runway you see two options to enter.")
    for index, path in enumerate(list_intro):
        # this is the loop that prints both options as well as the index value +1 (index starts @ 0).
        print(str(index + 1) + ".", path)
    # print("1. To enter through the drawbridge.")
    # print("2. To swim under the moat and enter the sewers.")


story = {}


def chosenpath():
    path = ""
    while path != "1" and path != "2":  # we are including input validation, meaning only predefined values will work.
        path = input(
            "What path will you choose? (1 or 2): ")  # the inclusion of the boolean operator "and" is also here

        return int(path)


def checkpath(path):  # here we are simply making a function to return a string based on the option the user chooses.
    print("You have chosen", list_intro[path - 1])
    return entering_path(path)


def entering_path(path):
    print(f"So you've entered {list_intro[path - 1]}")  # here we are simply using the user input to call a item from
    # our list using index values.
    if path == 1:
        return """You cross the drawbridge and see a gargoyle looking straight at you,
before you have time to react you feel a slight brush across your neck, you then fall to the ground
but see your body standing, it seems your head is no longer attached to your body.
Better luck next time!"""
    if path == 2:  # Adding a new string here for the other path.
        return """You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?"""
    print(hall_rooms)


def Dining_Hall_Rooms():
    print("So you've chosen", hall_rooms[-1])


intro()
path = chosenpath()
print(checkpath(path))

I get no error messages but when i run the code down the path of the "sewers" i get this -

You heard at the local tavern of a abandoned castle holding many treasures.
You journey for 7 days until you find yourself at the bottom of the path leading to the castle.
Standing at the foot of the castles runway you see two options to enter.
1. The drawbridge.
2. The sewers.
What path will you choose? (1 or 2): 2
You have chosen The sewers.
So you've entered The sewers.
You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?

Process finished with exit code 0

I'm sorry if I'm missing something really rather obvious, coding certainly isn't my strongest area but i really want to improve. Also apologies for the grammar errors.

5
  • What is the expected output? You've described where you are, but it's not clear what you want to achieve... Commented Oct 26, 2019 at 17:38
  • I would like it to then refer to a new set of outcomes based on the chosen door in the dining hall, i was planning to put a monster behind one and a treasure room behind the other. Meaning one would cause the game to be "won" and the other would call for a restart. I know that i haven't defined those yet but i was going to implement them later. What i want to happen is, just like on the 31st line where it says "you have chosen" i want it to say "So you have chosen the left/right door" when it ran, but so far it doesn't, it just prints the last string of where the player has entered the hall. Commented Oct 26, 2019 at 17:45
  • 1
    Surprise! Your code does exactly what you told it to do. See this [lovely debugging site]( ericlippert.com/2014/03/05/how-to-debug-small-programs) for help. Your overall flow works for exactly one room transition: you ask for an input of 1 or 2, call the corresponding function, and exit the program. Leaving out the narrative, this is all you've coded. Commented Oct 26, 2019 at 18:02
  • If you want to do something more than once… use a loop? Commented Oct 26, 2019 at 18:03
  • Also, keep the story line and the control flow separate. Learn enough programming to build your connected world move from area to area -- with your descriptions being only the simple ones you're already tracking well, such as "the sewers". Once your player can move through the plot, then you load in the narrative. Commented Oct 26, 2019 at 18:06

1 Answer 1

1

Your code structure here is making this a little bit awkward for you.

You have basically this:

def entering_path:
    if path == 1:
        return
    if path == 2:
        return
    print(“foo”)

But the final line, the print statement, will never get run if path is 1 or 2 because you have already returned a value from the function which exits it.

You need to either move the print statement out of that function, or make the function print your steps directly instead of returning a value that gets printed.

Instead of fixing your code though, I would suggest you you do a bit more research on program flow. You want your story (which is just data) to be stored separately from your logic and interacting with the user, and you want the logical flow to be easy to read.

Ideally, you should have a data structure containing the story and a function to parse that data, such that you can add or remove parts of the story without modifying the function and still have the full story work.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, i spoke to some people in my class and they too suggested a dictionary over what i was doing originally. I think I'll change to it, i don't have much experience with using them but ill see how it goes, thanks for the comment!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.