简体   繁体   English

Google Wave机器人/ Python变量问题

[英]Google Wave Robot / Python Variable question

I'm experimenting/having a little fun with wave robot python apiv2. 我正在尝试/使用Wave机器人python apiv2有点乐趣。

I made a little 8ball app for the robot which works fine, and now I'm trying to make a trivia app. 我为能正常工作的机器人制作了一个8ball小程序,现在我正在尝试制作一个琐事程序。

I've never programmed in Python but I'm pretty sure my syntax is correct. 我从来没有用Python编程过,但是我很确定我的语法是正确的。 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. 好的,基本上,我希望它能正常工作,以便当有人窃听“ strivia”时,该机器人识别出有人想要玩,因此它首先检查一个名为triviaStatus的变量,以查看我们是否已经在玩,然后从那里开始。 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. 为了使它起作用(实际上,此代码实际上是为了测试该问题),变量将需要像php $ _SESSION变量一样有效-也就是说,每次有人窃听时,它都会记住变量的值并且不会每次都重置。

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. 但是,不管是不是这种情况(如果不是这样,我都认为我可以通过将变量设置保存在txt文件或类似的东西中来做同样的事情),我感到困惑,因为上面的代码根本无法工作。 That is to say, the robot is not replying on !strivia or on !etrivia. 也就是说,机器人没有在!strivia或!etrivia上回复。 If the variables didn't save then if anything the robot should just reply with "Trivia Started" or with "Trivia is not running!" 如果变量没有保存,则机器人应使用“ Trivia Started”或“ 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. 如果我取消了triviaStatus的检查,则机器人会回复。 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 我也尝试制作一个!trivstatus,它会回复

"Trivia status is " + triviaStatus

but that ALSO choked up. 但这也使人窒息。 Why is it that every time I want to USE triviaStatus, the bot just dies? 为什么每次我要使用triviaStatus时,僵尸程序都会死掉? Note that I am able to SET triviaStatus fine (I just can't ever check what the output is by replying with it....) 请注意,我能够将triviaStatus设置为正常(我只是无法通过回复它来检查输出是什么...。)

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! 谢谢!

It seems that you should rename triviaStatus to trivia_status and make sure that trivia_status has some value eg, bind it to None before the first use. 似乎您应该将triviaStatus重命名为trivia_status并确保trivia_status具有某个值,例如,在首次使用前将其绑定为None Otherwise your code might raise UnboundLocalError or NameError exceptions due to triviaStatus / trivia_status doesn't refer to any object. 否则,由于triviaStatus / trivia_status不引用任何对象,您的代码可能会引发UnboundLocalErrorNameError异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM