Ex 52 - 10 Tries Laser Weapon Armory

Hello,

I am currently working on Ex 52 of learn python the hard way and am trying to figure out how to implement the 10 tries for the safe code in the laser weapon armory. After a couple attempts I am currently at this point where I try to use a while loop in the app.py code if the current session is 'laser_weapon_armory and the count is less than 9. When I put print in the code I can see that I am in fact entering the loop but that the web page does not reload after this first attempt (the count simply increases each loop until it reaches the exit point and then I go to the death scene). Any input on what I could do differently would be greatly appreciated.

In the mean time I will continue to try to work through this problem.

#
from flask import Flask, session, redirect, url_for, escape, request
from flask import render_template
from gothonweb import planisphere


app = Flask(__name__)

@app.route("/")
def index():
    #this is used to "setup" the session with starting values
    session['room_name'] = planisphere.START
    return redirect(url_for("game"))

@app.route("/game", methods = ['GET', 'POST'])

def game():
    count = 0
    room_name = session.get('room_name')

    if request.method == "GET":
        if room_name:
            room = planisphere.load_room(room_name)
            return render_template("show_room.html", room = room)
        else:
            # why is this here? do you need it?
            return render_template("you_died.html")
    else:
        current_room = planisphere.load_room('laser_weapon_armory')
        while session['room_name'] == 'laser_weapon_armory' and count <= 9:
            count +=1
            print(count)
            session['room_name'] = current_room.go('stay')
            action = request.form.get('action')
        action = request.form.get('action')

        if room_name and action:
            room = planisphere.load_room(room_name)
            next_room = room.go(action)
            death_room = room.go('*')

            if not next_room:
                session['room_name'] = planisphere.name_room(death_room)
            else:
                session['room_name'] = planisphere.name_room(next_room)

        return redirect(url_for("game"))


if __name__ == "__main__":
    app.run()

edit: forgot to mention that in session[‘room_name’] = current_room.go(‘stay’) I made a path in planispheres for the laser_weapon_armory that maps ‘stay’ to laser_weapon_armory in an attempt to try to reload that original page

See how you have:

session['room_name'] = planisphere.name_room(next_room)

You’re probably losing the count on each page load because HTML/HTTP doesn’t maintain any state. That’s what the session is doing in the above line. It’s storing what the web app “remembers” about the person so that when they come back it’ll be like they never left.

So I think if you do this:

session['laser_weapon_count'] = 0

Then work that variable it’ll work.

Thank you for the reply zed. I went back two exercises because I feel like I was not fully understanding what the code in the app file was doing. I am still working through these to make sure I fully understand. Once I get back to ex 52 I will try to implement a session variable that counts.

That’s always a good idea. Also I’m a big fan of doing the exercises twice. It seems if you do it once you don’t believe you actually did it, but doing it again makes you confident that you really did it.

A free service run by Zed A. Shaw for learncodethehardway.org.