I need help with Ex 52

I’m currently on the last exercise and can not understand what the " couple of problems " that Mr. Shaw mentioned after the planisphere.py code are. I am completely lost here.:disappointed_relieved:

I’m also on Ex52 and encountering a few issues but maybe can offer my input.

Are you referring to the global variables? They are problematic as they call variables outside of the scope of the method/function. A variable inside of a class or function is limited to the class/function. With the use of the global variable call, Python is able to use variables without this limitation. Although this sounds useful, these global calls can be exploited to reveal variable values which should remain hidden etc.

I tried to overcome the problem by using a dictionary within a class which returned the Room but encountered issues with the object being returned.

I hope this helps.


Hey, if you two have your code handy I can try to refactor it for you this week. I do a show where I demonstrate how to fix, debug, and refactor code. And @Lukehog best thing is to post a quote of what you’re referring to so I know I’m giving you the right advice.

1 Like

Yes, sure.

This is my app.py file.

    # create 'app' an instance of Flask
    app = Flask(__name__)
    # create session secret key
    app.secret_key = 'Ex52'
    # create root URL which redirects to /game
    def index():
        # creates session, takes the var @ start in planisphere.
        session['room_name'] = START
        return redirect(url_for('game'))
    # create route for /game which takes methods 'POST', 'GET'
    @app.route('/game', methods = ['POST', 'GET'])
    def game():
    # room name is taken from the session name
    room_name = session.get('room_name')
    # if page method is 'GET' invoke indented code.
    if request.method == 'GET':
        # if room_name exists, create var room - val from load_room(name_name)
        # room = load_room(START) = 'central_corridor'
        if room_name:
            room = rooms.load(room_name)
            return render_template('show_room.html', room = room)
            return render_template('you_died.html')

        action = request.form.get('action')

        if room_name and action:
            room = rooms.load(room_name)
            next_room = room.go(action)

            if not next_room:
                session['room_name'] = rooms.load(room)
                session['room_name'] = rooms.load(next_room)

        return redirect(url_for('game'))

    if __name__ == '__main__':
    app.run(debug = True)

And this is my planisphere.py

    # create class 'Room'
    class Room(object):
        # define __init__ self function, accepts 'name', 'description'
        def __init__(self, name, description):
            # maps param 'name' to 'self.name = name' and 'self.description = description'
            # 'self.paths = {}'
            self.name = name
            self.description = description
            self.paths = {}

        # define function go, accepts params 'self' and 'direction', it calls itself
        # using self.paths.get returns the values within the dictionary assigned to
        # Rooms.paths
        def go(self, direction):
            return self.paths.get(direction, None)

        # define function 'add_paths', accepts params 'self' and 'paths' which calls
        # 'self.paths.update' which is used to update the dictionary in the class
        # Room.paths
        def add_paths(self, paths):

    central_corridor = Room('Central Corridor',
    The Gothons of Planet Percal #25 have invaded your ship and
    destroyed your entire crew. You are the last surviving member
    and your last mission is to get the neutron destruct bomb
    from the Weapons Armoury, put it in the bridge, and blow up
    the ship up after getting into an escape pod.

    You're running down the central corridor to the weapons Armoury
    when a Gothon jumps out. Red scaly skin, dark grimy teeth, and
    evil clown costume flowing around his hate-filled body. He's
    blocking the door to the Armoury and about to pull a weapon to
    blast you.

    laser_weapon_armoury = Room('Laser Weapon Armoury',
    Luckily, they make you learn Gothon insults while in the
    academy. You tell the one Gothon joke you know:
    'Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr,
    fur fvgf nebhaq gur ubhfr'. The Gothon stops. It tries not
    to laugh but eventually it bursts out laughing and cant'
    move. While he's laughing you run up and shoot the Gothon
    square in the head, putting it down before jumping through
    the Weapon Armoury door.

    You do a diving roll into the Weapon Armoury before crouching
    and scanning the room for more Gothons that might be hiding
    It's dead quiet, too quiet. You stand up and run to the far
    side of the room and find the neutron bomb in it's container.
    There's a keypad lock on the box and you will need the code
    to retrieve the bomb. If you get the code wrong 10 times then
    the lock closes and can never be opened. The code is 3 digits.

    the_bridge = Room('The Bridge',
    The container clicks open and the seal breaks with a hiss
    as the gas escapes. You grab the neutron bom and run as
    fast as you can to the bridge where you must place it in
    the right spot.

    You burst onto the Bridge with the neutron bomb under your arm.
    You surprise 5 Gothons who are trying to take control of the ship.
    Each of them has an even uglier face and costume than the last.
    They haven't pulled their weapons yet as they have seen the active
    bomb under your arm and don't wish to set it off.

    escape_pod = Room('Escape Pod',
    You point your blaster at the bomb under your arm and the Gothons
    put their hands up and start to move backward. Sweat beads on their
    large grotesque foreheads. You inch towards the door, open it, and
    then place the bomb on the floor, pointing your blaster at it. You
    then jump backwards through the door, punching the close button.
    You blast the lock so the Gothons can't get out. Now that the bomb
    placed, you run to the escape pod to get off this tin can.

    You rush through the ship desperately trying to make it to the escape
    pod before the ship explodes. It seems like hardly any Gothons are onboard
    the ship so you are left unhindered. You get to the chamber with the escape
    pods and now need to pick one. Several have have been damaged, some more than
    others, but you don't have time to check them all. There's 5 pods, which one
    do you take?

    the_end_loser = Room('The End',
    You jump into pod {guess} and hit the eject button. The pop escapes out
    into the void of space then implodes as the hull ruptures, crushing your
    body into human jam.
    the_end_winner = Room('The End',
    You jump into pod {guess} and hit the eject button. The pod easily
    slides out into space heading to the planet below. As it flies to
    to the planet, you look back and see your ship explode like a bright
    star, taking out the Gothon ship at the same time. You won.

                        '2': the_end_winner,
                        '*': the_end_loser

    generic_death = Room('Death', 'you died')

                        'throw the bomb': generic_death,
                        'slowly place the bomb': escape_pod

                                    '0132': the_bridge,
                                    '*': generic_death

                                'shoot!': generic_death,
                                'dodge!': generic_death,
                                'tell a joke': laser_weapon_armoury

    START = 'Central Corridor'

    class rooms():
        def __init__(self, name):
            self.name = name

        def load(name):
            rooms = {
                    'Central Corridor': central_corridor,
                    'Escape Pod': escape_pod,
                    'The Bridge': the_bridge,
                    'Laser Weapon Armoury': laser_weapon_armoury,
                    'Death': generic_death,
                    'The End': the_end_loser,
                    'The End': the_end_winner
            room = rooms[name]
            return room
        def name():
            rooms = {
                    'Central Corridor': central_corridor.name,
                    'Escape Pod': escape_pod.name,
                    'The Bridge': the_bridge,
                    'Laser Weapon Armoury': laser_weapon_armoury.name,
                    'Death': generic_death.name,
                    'The End': the_end_loser.name,
                    'The End': the_end_winner.name
            room = rooms[name]
            return room
    # def load_room(name):
    #     return globals().get(name)
    # def name_room(room):
    #     for key, value in globals().items():
    #         if value == room:
    #             return key

While using a dictionary to hold the key and values that refer to the Rooms, the object ‘Room’ in app.py returns a JSON error within flasks debugging.

Thank you for taking a look.

Hmmm, I could go off that but do you feel like trying to get this up on github.com? It could be a very valuable skill to learn, and when you’re done I can then take it and make changes and you can see exactly what I did.

Yep, of course. I’ll just get it up online and get back with the link.

This is my GitHub repository for my LPTHW exercises.



Ok great, BUT, don’t wait around for me to solve it for you. Keep trying to figure it out.

I haven’t stopped trying to solve it as I want to try and find a solution myself.

Thank you, but I finally figured it out myself . Sorry that I am replying after a very long time. I just completed my exams. I will make sure that I add quotes and screenshots to make my question clear.

Once again, thank you.

Nice, can you summarize your solution?

It would be really helpful if you could share an outline of your solution. I’m stuck on the global variables right now as well.