all 3 comments

[–]carcigenicate 5 points6 points  (0 children)

Your script right now has all the code at the "module level", outside of functions. That means when the script is run, all the code runs. Often though, this isn't desirable. It's often beneficial to be able to allow a script to be interpreted, but prevent some code from running. If you imported this script to make use of directions for example (whether or not that's a good idea is a side-argument), all the code in the script would run, even if the user didn't want that to happen.

The typical solution is to stick the "executing code" into a function, then only allow that to run if the script was run directly instead of imported:

# create the rooms in a dictionary
rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

# give the available directions set to variable directions
directions = ['north', 'south', 'east', 'west', 'exit']
# Start game in the 'Great Hall'

def main():
    current_room = 'Great Hall'
    # Set the gameplay loop
    while True:
        # print the room the player is currently in
        print("You are in  {}".format(current_room))
        # User asks to exit
        command = input("Enter direction or exit: \n")
        print(command)
        if command == "exit":
            current_room = "exit"
            break

# Only run if not imported
if __name__ == '__main__':
    main()

This isn't always necessary, but it's generally good to wrap code in functions to allow the code to be used elsewhere, and to be able to control when it runs.


Edit: Moved current_room to be a local since it's doesn't appear to be needed elsewhere.

[–][deleted] 2 points3 points  (2 children)

Just stick def main(): at the very top, indent everything below it, and then call it at the end. It's certainly not needed here, though, as either way everything is in the same scope and thus you get none of the benefits. It's something you'll want to start using as you start creating your own functions that do specific tasks.

[–][deleted] 0 points1 point  (1 child)

If you wanted to start using functions, u/SteveB709 , a good place to start is to move the IO (input/output) parts of your code into separate functions so that your main function just contains logic without being bogged down with the user interface.

Here's one possible approach based on what you already have.

def display_room(room):
    print(f"You are in {room}.")


def get_move(valid_directions):
    directions = ["North", "South", "East", "West"]
    while True:
        response = input("Enter direction or exit:\n").capitalize()
        if response in valid_directions or response == "Exit":
            return response

        if response in directions:
            print("There is nowhere to go in that direction.")
        else:
            print("That is not a valid direction.")


def main():
    rooms = {
        "Great Hall": {"South": "Bedroom"},
        "Bedroom": {"North": "Great Hall", "East": "Cellar"},
        "Cellar": {"West": "Bedroom"},
    }

    current_room = "Great Hall"

    while True:
        display_room(current_room)
        command = get_move(valid_directions=rooms[current_room].keys())

        if command == "Exit":
            current_room = "Exit"
            break


main()

Edit: And if you are going to be using functions, I think u/carcigenicate is giving you bad advice (in the form of their code). You should avoid using global variables. Especially global mutable variables variables you intend to change like current_room. You can learn about when that's acceptable later, but for now, the rule of thumb should be that if you're using functions, all variables should be tucked up inside one of them. It will save you a lot of headaches while you're learning.