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!

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
Greg
  • 11
  • 1
  • Ahhh sorry...I don't know why the code looks all weird. It looked fine in the preview! Here are screenshots of the code which should make it easier to read. Code that does not work (first one I posted): http://img341.imageshack.us/img341/575/doesnotworkpython.png Code that works (third one I posted): http://img202.imageshack.us/img202/5096/workspython.png Thanks! – Greg May 18 '10 at 20:05
  • s/triviaStatus/trivia_status/ – jfs May 18 '10 at 20:07
  • @Greg: indent the code using 4 spaces (or select the code and push the corresponding button in the editor) – jfs May 18 '10 at 20:09
  • 1
    `()` are unnecessary for `if/elif/` expressions. – jfs May 18 '10 at 20:10
  • I'd create a dictionary with functions such as `dict(strivia=self.cmd_strivia, etrivia=self.cmd_etrivia)()` and put it in `self.message_received` or something. – jfs May 18 '10 at 20:16
  • Wow, impressive how quickly you people comment here haha! Yeah the variable name changed between the two expressions, I was just seeing if for some reason the underscore was screwing things up (it wasn't...made no difference). So the () are unnecessary but are they actually breaking the code? I imagine not because some of the other robot code uses if()s and those parts work fine. I didn't quite understand your last comment, unfortunately. What would that do? What is self.message_received/what does it do? – Greg May 18 '10 at 20:24
  • No, `()` do not break the code. In the last comment I suggested to create a class `Bot` with methods cmd_strivia, cmd_etrivia, message_received. `message_received()` does the job of the `if/elif/else` statements but it delegates actual replies to `cmd_*` methods. – jfs May 18 '10 at 20:34
  • @Greg: If you don't know what the class is then just forget it. It is not relevant for the question. – jfs May 18 '10 at 20:36

1 Answers1

1

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 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 May 18 '10 at 20:39
  • @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) – jfs May 18 '10 at 20:55
  • @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. – jfs May 18 '10 at 21:10