我已经使用套接字设置了一个 irc 机器人。我添加了一些命令,但我想添加一个“轮询”功能。 理想情况下,机器人会收到格式如下的命令:
!poll <name> <opt1> <opt2> <opt3> <time>
我将如何检查投票的用户并在特定时间后结束投票?
提前致谢
绝望的 Python 初学者。
编辑:非常感谢大家的回复,我使用了全局变量(我知道,我知道),因为我不知道如何做。再次,非常感谢!
最佳答案
好吧,我开始对我的 Python 有点生疏了,但我想我可以回答这个问题——尽管这可能不是最好的答案。
如果您计划同时进行许多民意调查,您可以实现一个字典,其中包含自定义类的多个实例,例如 Poll..这是一个可能的解决方案:
class PollVotes(object):
def __init__(self):
self.votes = []
self.stoptime = "some date/time" #I can't remember how to do this bit ;)
def add_vote(self, vote_value):
self.votes.append(vote_value);
def tally_votes(self):
return self.votes.size()
def has_closed(self):
if time_now >= self.stoptime: # I forget how you'd do this exactly, but it's for sake of example
return True
else:
return False
#then use it something like this
poll_list = {}
#irc processing...
if got_vote_command:
if poll_list["channel_or_poll_name"].has_ended():
send("You can no longer vote.")
else:
poll_list["channel_or_poll_name"].add_vote(persons_vote)
#send the tally
send("%d people have now voted!" % poll_list["channel_or_poll_name"].tally_votes())
当然,您必须编辑投票类以满足您的需要,即允许投票中有多个值,记录谁在投票什么(如果您想要的话)等。
至于检查投票是否已经结束,您可以编辑投票类以具有停止时间,并有一个函数返回 True/False 是否该时间已过。可能查看日期时间模块的文档...?
无论如何,希望这对您有所帮助。
关于python - 我如何通过 irc 机器人进行投票?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5463118/