Using variables in application without refreshing?

63 views
Skip to first unread message

Ash Courchene

unread,
Jun 30, 2012, 12:04:06 PM6/30/12
to we...@googlegroups.com
Hi Everyone,

Im trying to put together this number guessing "game" together as part of a larger "text-based adventure game" app (the final project of Learn Python the Hard Way), and am wondering if there was a way change a variable in my application without the page being refreshed. I understand that html files are "stateless", so that may be affecting why when I write this code (or a variation of) below, it doesn't work.
    
    num, guess = session.room.generate()  # a function used to generate a random number for the user to guess and how many tries they get.
    if form.action != num:  # form.action is user input
        guess -= 1
        session.room.output = "BZZT. Access Denied. You have %d tries left." % guess  # session.room.output is what the user sees as a result of their actions
        if guess == 0:
            session.room = session.room.go('*')    # moves user to a game over screen, pretty much.
    elif form.action == num:
        session.room = session.room.go('next')
    ....

I've thought about using session variables, but even after i set one up (and I don't even know if i did it correctly), the result is the same. session.room.output always stays at nine. and the user never gets out of that "room". I don't know if this makes much sense...
But does anyone know how I can achieve getting the variable "guess" down to zero if the user doesn't input the correct number? Thanks. 

Tomas Schertel

unread,
Jun 30, 2012, 5:02:04 PM6/30/12
to we...@googlegroups.com
As far as I know, the only way to change anything in html is using ajax.

Ash Courchene

unread,
Jul 1, 2012, 12:26:56 PM7/1/12
to we...@googlegroups.com
Damn it...
Ok then, so the question now becomes does anyone know where I can learn how to use ajax with web.py in respects to what I want accomplished?
(If the user does not guess the specified number within a certain amount of tries, then its game over?)

Primoz Anzur

unread,
Jul 1, 2012, 12:46:16 PM7/1/12
to we...@googlegroups.com
Well... You can set a start time in session, and you can repeatedly check numberof moves in x time via ajax.
Your best bet would most likely be messing with JQuery or something in that direction...

--
You received this message because you are subscribed to the Google Groups "web.py" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/VJDtAAeXfNcJ.

To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

Shannon Cruey

unread,
Jul 1, 2012, 6:10:25 PM7/1/12
to we...@googlegroups.com, we...@googlegroups.com
Tomas and I had a similar conversation just a few weeks ago on this thread. I'm traveling and can't pull it up on my phone but maybe you can dig back on the google group and find it. 

I put code examples in that thread. 

Hope it helps!

A


Tomas Schertel

unread,
Jul 2, 2012, 12:06:27 PM7/2/12
to we...@googlegroups.com
In my case I was trying to populate a dropbox with values stored on a database.
I solved it using a JQuery plugin called JCombo.
But Primoz had a good start to help you.
You could call a js function to verify values when a event occur. Like a change in a combobox, or time based.
BUT, all this ideas requires javascript knowledge. I say go for JQuery.
To unsubscribe from this group, send email to webpy+unsubscribe@googlegroups.com.

Ash Courchene

unread,
Jul 5, 2012, 12:21:36 PM7/5/12
to we...@googlegroups.com
Actually, I found an way to change the variable just using state variables (I.e., class parameters)

class Room(object):

def __init__(self, number=None, tries=None):
self.num = number
self.guess = tries

and then i refracted the code I displayed in my original post, so it worked:

if form.action != session.room.num and form.action not in self.commands: # session.room is room object
session.room.guess -= 1
session.room.output = "BZZZTT!! Access Denied. You have %d tries left" % session.room.guess
if session.room.guess == 0:
session.room = session.room.go('*') #go function built-in to Room class and '*' is the key in dict that maps out the room 'path'
elif form.action == session.room.num:
session.room = session.room.go('x') #'x' the name of key in same dict that maps out the room 'path'

Reply all
Reply to author
Forward
0 new messages