类中的循环否定它之前的代码 [英] loop within a class negates the code before it

查看:48
本文介绍了类中的循环否定它之前的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中,问题是如何实现一个条件循环(一个简单的 for 循环)来影响 onMessage 事件调用函数,已解决.但是,我当前的问题是尝试将时间作为条件循环中的行列式.

In a previous question, the issue was how to implement a conditional loop (a simple for loop) to affect the onMessage event calling function, which was resolved. However, my current issue is in trying to implement time as the determinant in the conditional loop.

目前,代码运行但似乎只执行重新检查时间的 while 循环,而忽略了它之前的代码.我怀疑问题出在我放置时间重新检查循环的位置?

Currently, the code runs but only seems to do the while loop that rechecks the time, ignoring the code before it. I suspect that the issue is in the location that I have placed the time rechecking loop?

对此问题的任何澄清将不胜感激!

Any clarity on this issue would be appreciated!

import ch
import time

class bot(ch.RoomManager):
    timeleft = 0
    starttime = 0

    def onMessage(self, room, user, message):
        print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
        if 100 >= timeleft >= 1:
            print('success')
        else:
            print('fail')

    loop = 1
    while loop == 1 :
        timeleft = starttime - int(time.clock())
        if timeleft <=0:
            timeleft = 200
            starttime = 200

rooms = ["testgroup444"]
username = "user"
password = "name"

bot.easy_start(rooms,username,password)

为了清楚说明所使用的方法,可以在此处找到整个 ch.py​​ 库:https://pastebin.com/8ukS4VR1

For clarity on the methods used, the entire ch.py library can be found here: https://pastebin.com/8ukS4VR1

推荐答案

我认为您仍然对 Python 语法有问题.在 Python 中,空格非常重要,因为它定义了作用域.编写代码的方式是,while 循环在定义类时执行,然后才开始.

I think you still have an issue with the Python syntax. In Python whitespace is very important as it defines scope. The way your code is written, the while loop is executed at the time the class is defined, before anything is started.

此外,代码进入无限循环,因为while loop == 1 将始终为True.这就是为什么你看到你的代码卡住了.从评论中的讨论来看,我想你想写一些类似的东西:

Further, the code gets into an infinite loop, as while loop == 1 will be always True. This is why you see that your code gets stuck. From the discussion in the comments, I imagine you want to write something like:

import ch
import time
import enum

class State(enum.Enum):
    idle = 0
    nominating = 1
    voting = 2
    watching = 3

class bot(ch.RoomManager):

    state = State.idle
    movie_length = 20

    def updateState(self):

        if self.state in [State.idle, State.nominating]:
            self.state = State.voting
            timeout = 10
        elif self.state == voting:
            self.state = State.watching
            timeout = self.movie_length - 15
        else: # if self.state == watching
            self.state = State.nominating
            timeout = 15

        self.setTimeout(timeout*60, bot.updateState, self)

    def onConnect(self, room):
        # Redirect through event loop, not strictly nessecary
        self.setTimeout(0, bot.updateState, self)

    def onMessage(self, room, user, message):

        print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
        print("Current state is {0}".format(self.state))

rooms = ["testgroup444"]
username = "user"
password = "name"

bot.easy_start(rooms,username,password)

这里使用了 ch (bot) 类中定义的 setTimeout 方法,以允许在特定时间传递消息.每次超时都会更新状态.然后在所有内部方法中都可以使用实际状态,例如在 onMessageupdateState 中.

Here, the setTimeout method defined in the ch (bot) class is used, to allow messages to be passed at certain times. At every timeout the state is updated. The actual state is then available in all internal methods, e.g. in onMessage and updateState.

因为我不使用聊天网络或客户端,所以我不能保证该解决方案有效.

As I do not use the chat network or client I cannot guarantee that the solution works, though.

这篇关于类中的循环否定它之前的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆