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.

Hello Zed,

Just wanted to give an update on where I am now. I also reworked some of the older examples and then got caught up taking a few online programming courses. Finally made it back to this problem today and I took the time to go back through and comment all the code. This way I could have a better understanding of what it was doing.

Below is the new code I wrote that addressed the problem of counting the users tries. Indeed, making a session variable was the answer. I also changed the previous code I had because when I came back to it I did not like the way it was written:

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

app = Flask(__name__)

@app.route("/")
def index():
    #this is used to "setup" the session with starting values
    #create a session variable called room_name and set it to equal 'central_corridor'
    session['room_name'] = planisphere.START
    #create the session count variable and set it equal to 0
    session['count'] = 0
    #redirect to url game
    return redirect(url_for("game"))

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

def game():
    #to start room_name is set to session.get('room_name') which equals 'central corridor'
    room_name = session.get('room_name')

#if the page is loaded with "GET" method
if request.method == "GET":

    #if room_name i.e. session.get('room_name') is not blank
    if room_name:
        #get the object that has the 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")

# otherwise we got it with the POST method (i.e. form submit)
else:
    action = request.form.get('action')

    if room_name and action:
        #set the room equal to the room object
        room = planisphere.load_room(room_name)
        #get the action from that room
        #for ex. room = central_corridor object room.go('shoot') = generic_death object
        next_room = room.go(action)


        #if next room is None either action wasnt in paths or nothing was
        # submitted. Check action is blank '' and if it was reload same page
        if not next_room and action == '':
            session['room_name'] = planisphere.name_room(room)

        #if the action is not blank and not in paths
        #and we are in the laser weapon armory
        #we want to give the player 10 chances to get the code right
        #after 10 chances he dies, before then load the same room
        elif not next_room and room_name == 'laser_weapon_armory' and session['count'] < 9:
            session['count'] += 1
            next_room = room.go('stay')
            session['room_name'] = planisphere.name_room(next_room)


        #if the action is not blank and not in paths
        #set next_room = room.go('*') to return a generic death
        elif not next_room:
            next_room = room.go('*')
            session['room_name'] = planisphere.name_room(next_room)

        #otherwise action was submitted and in paths so
        #load the next room from the action
        else:
            session['room_name'] = planisphere.name_room(next_room)

    #redirect the url for the new page
    return redirect(url_for("game"))

Now I will work on adding the parser and implementing the other features you mentioned at the end of the exercises. Hopefully this helps anyone who was stuck on this part of the problem in the future. Best regards.

Nice, that looks like it works. I’d say take out the pseudo-comments and replace them with more meaningful descriptions of why you’re doing that, and then see if it still holds up when you can see all of the code on its own. Sometimes your comment tricks your brain into thinking the code is right. For example, let’s say you have this:

# for each person in trucks, open door
for person in trucks: person.close_door(truck)

Did you see it? In the comment I said “open door”, but in the code I wrote close_door. The pcode helps you get it started, but it can confuse you if you leave it, so always clean it out once you don’t need it and rewrite comments to match the code.

Ok, I’ll be sure to clean it up when implementing these next sections for sure.

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