views:

49

answers:

1

I'm experimenting/having a little fun with wave robot python apiv2.

I made a little 8ball app for the robot which works fine, and now I'm trying to make a trivia app.

I've never programmed in Python but I'm pretty sure my syntax is correct. Here is the relevant code:

elif (bliptxt == "\n!strivia"):
  reply = blip.reply()
  if (triviaStatus != "playing"):
   reply.append("Trivia Started!")
   triviaStatus = "playing"
  else:
   reply.append("Trivia is already running!")
elif (bliptxt == "\n!etrivia"):
  reply = blip.reply()
  if (triviaStatus == "playing"):
   reply.append("Trivia Ended!")
   triviaStatus = "stopped"
  else:
   reply.append("Trivia is not running! To start trivia, type !strivia")
else: (snipped out)

Okay so basically I want it to work so that when someone blips "strivia" the bot recognizes that someone wants to play so it first checks a variable called triviaStatus to see if we are already playing and goes from there. Pretty simple stuff.

In order for this to work (and, actually, this code is really meant to test this question out) the variables would need to effectively be like the php $_SESSION variables - that is, it remembers the value of the variable every time someone blips and does not reset each time.

Nevertheless, whether or not that is the case (if it isn't then I assume I can do the same thing by saving variable settings in a txt file or something) I am baffled because the code above does not work at all. That is to say, the robot is not replying on !strivia or on !etrivia. If the variables didn't save then if anything the robot should just reply with "Trivia Started" or with "Trivia is not running!" each time. But it just does not reply at all.

If I remove the check for triviaStatus, the robot DOES reply. But then there's no logic and I can't test my question out.

I also tried making a !trivstatus where it would reply back with

"Trivia status is " + triviaStatus

but that ALSO choked up. Why is it that every time I want to USE triviaStatus, the bot just dies? Note that I am able to SET triviaStatus fine (I just can't ever check what the output is by replying with it....)

So, to sum this up...how come the above code does not work but the following code DOES work:

elif (bliptxt == "\n!strivia"):
  reply = blip.reply()
  reply.append("Trivia Started!")
  trivia_status = "playing"
elif (bliptxt == "\n!etrivia"):
  reply = blip.reply()
  reply.append("Trivia Ended!")
  trivia_status = "stopped"

Thanks!

+1  A: 

It seems that you should rename triviaStatus to trivia_status and make sure that trivia_status has some value e.g., bind it to None before the first use. Otherwise your code might raise UnboundLocalError or NameError exceptions due to triviaStatus/trivia_status doesn't refer to any object.

J.F. Sebastian
That worked!Unfortunately though, as I expected the variable just resets every time someone sends in a new blip, so I'll probably have to store values in a text file. (There is no other way to do this, right?)
Greg
@Greg: It is hard to tell without more context. What code do you use as a base e.g., http://wave.to/guides/simple-robots-v2/blippy http://code.google.com/apis/wave/extensions/robots/python-tutorial.html#Hello_Robot You could use globals (event handler is a function) or instance attributes (event handler is an object)
J.F. Sebastian
@Greg: if each blip is processed in different process (cgi model) then you need to use some kind of persisent data storage/database e.g. `google.appengine.ext.db` on App Engine. Whether you may write flat text files depends on where your script is deployed.
J.F. Sebastian