2

我的机器人程序一直有这个错误:

start.py:890 分配前引用的局部变量 'cmd_part_1'

代码:

try:
    my_alias = message.body.split(" ", 3)
    if len(my_alias) > 2:
        cmd_part_1 = my_alias[0]
        cmd_part_2 = my_alias[1]
    elif len(my_alias) < 2:
        cmd_part_1 = ""
        cmd_part_2 = ""
except Exception as e:
    cmd_part_1 = ""
    cmd_part_2 = ""

if self.getAccess(user.name.lower()) >= lvl_config.rank_req_callme and cmd_part_1 == "call" and cmd_part_2 == "me":
    whole_body = message.body
    whole_body = whole_body.replace("call me ", "");
    whole_body = whole_body.replace(",", "&#44;");
    chat_message("<font color='#%s' face='%s' size='%s'>%s <b>%s</b></font>" % (font_color, font_face, font_size, random.choice(["Recorded! ^_^ I will now call you", "Registah'd, you are now", "Yo, dis mah big homie, I call dem", "Ye-a-a-ah, I guess I can call you that...", "If I have to.. I suppose I'll call you..", "I decided I will call you"]), whole_body), True)
    alias_flag = 0
    finished_proc = 1
    file = open("storage/flatfile/various/aliases.csv", "r")
    for line in file.readlines():
        alias_data = line.strip()
        alias_username, alias_nickname = alias_data.split(",", 1)

错误行:

if self.getAccess(user.name.lower()) >= lvl_config.rank_req_callme and cmd_part_1 == "call" and cmd_part_2 == "me":

我究竟做错了什么?

4

1 回答 1

3

您在set 的第一个块中有ifandelif语句。trycmd_part_1

如果这些if语句中的条件都不是 会发生什么True

在这种情况下,cmd_part_1永远不会被赋值。这就是您的代码中发生的事情。解决这个问题,它会工作。也许在else那里添加一个子句并为两者分配一个默认值cmd_part_1cmd_part_2。或者让其中一个拥有=.

例如:

if len(my_alias) >= 2:

代替:

if len(my_alias) > 2:

之后,正如 eryksun 在下面的评论中建议的那样,您可以将 替换elifelse.

于 2013-03-09T01:30:10.050 回答