Search code examples
pythonadventure

Struggling with my text based game in python


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.


Solution

  • 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.