UnboundLocalError:局部变量' msg'在分配之前引用。怎么解决?

时间:2018-03-26 21:28:27

标签: python python-3.x discord discord.py

我从json文件中获取消息的数量,但我有一个错误......我不知道为什么。在这个程序中,bot必须发送有关成员的信息。如果我在代码的开头写msg = None,它就不会发送> messages = 10它会发送messages = None

PROGRAM

if args[0] == '!in':
    await client.delete_message(message)
    highclass = None
    gm = ""
    clan = None
    if (roleLeaders in message.author.roles or roleHunter in message.author.roles or roleDeputy in message.author.roles):
            for member in message.mentions:
                user = member
                userjoin = str(user.joined_at)
                joinpars= userjoin.split()
                join = joinpars[0]
                msg1 = user_get_msg(member.id)
                time1 = user_get_time(member.id)
                s = str(msg1)
                last = int(s[-1])
                st = str(time1)
                stm = time1 // 60
                sth = stm // 60
                std = sth // 24
                if time1 <= 60:
                    lastt = int(st[-1])
                    if lastt == 0:
                        time = '{0} секунд'.format(time1)
                    if lastt == 1 and not time1 == 11:
                        time = '{0} секунду'.format(time1)
                    if ((time1 >= 11 and time1 <= 19) or (lastt >= 5 and lastt <= 9)):
                        time = '{0} секунд'.format(time1)
                    if (lastt >= 2 and lastt <= 4) and not (time1 >= 11 and time1 <= 19):
                        time = '{0} секунды'.format(time1)

                if time1 <= 3600 and time1 > 60:
                    lastt = int(str(stm)[-1])
                    if lastt == 0:
                        time = '{0} минут'.format(stm)
                    if lastt == 1 and not stm == 11:
                        time = '{0} минуту'.format(stm)
                    if ((stm >= 11 and stm <= 19) or (lastt >= 5 and lastt <= 9)):
                        time = '{0} минут'.format(stm)
                    if (lastt >= 2 and lastt <= 4) and not (stm >= 11 and stm <= 19):
                        time = '{0} минуты'.format(stm)

                if time1 <= 86400 and time1 >3600:
                    lastt = int(str(sth)[-1])
                    if lastt == 0:
                        time = '{0} часов'.format(sth)
                    if lastt == 1 and not sth == 11:
                        time = '{0} час'.format(sth)
                    if ((sth >= 11 and sth <= 19) or (lastt >= 5 and lastt <= 9)):
                        time = '{0} часов'.format(sth)
                    if (lastt >= 2 and lastt <= 4) and not (sth >= 11 and sth <= 19):
                        time = '{0} часа'.format(sth)

                if time1 >= 86400:
                    lastt = int(str(std)[-1])
                    if lastt == 0:
                        time = '{0} дней'.format(std)
                    if lastt == 1 and not std == 11:
                        time = '{0} день'.format(std)
                    if ((std >= 11 and std <= 19) or (lastt >= 5 and lastt <= 9)):
                        time = '{0} дней'.format(std)
                    if (lastt >= 2 and lastt <= 4) and not (std >= 11 and std <= 19):
                        time = '{0} дня'.format(std)
                if msg1 >= 0:
                    if last == 0 or msg1 == 0:
                        msg = '{0} записок'.format(msg1)
                    if msg1 == 1 or (last == 1 and msg1 >= 21):
                        msg = '{0} записку'.format(msg1)
                    if (msg1 >= 2 and msg1 <= 4) or (msg1 >= 22 and msg1 <= 24):
                        msg = '{0} записки'.format(msg1)
                    if (last >= 5 and last <= 9):
                        msg = '{0} записок'.format(msg1)
                for role in member.roles:
                    with open('class.json', 'r') as roleread:
                        if role.id in roleread.read():
                            highclass = '<@&{0}>'.format(role.id)
                    with open('clans.json', 'r') as fp:
                        if role.id in fp.read():
                            gm = '> Является участником гильдии <@&{0}>.'.format(role.id)
                            if roleGM in member.roles:
                                gm = '> Является **мастером** гильдии <@&{0}>.'.format(role.id)
                await asyncio.sleep(0.01)
                await client.send_message(message.channel, 'Персонаж {0}:\n'
                                                           '> Прибыл в город **{1}**;\n'
                                                           '> Принимал участие в жизни города **{2}**;\n'
                                                           '> Написал **{3}**;\n'
                                                           '> Имеет класс {4};\n'
                                                           '{5}'.format(member.mention, join, time, msg, highclass, gm))

ERROR

File "soul.py", line 497, in on_message
'{5}'.format(member.mention, join, time, msg, highclass, gm))
UnboundLocalError: local variable 'msg' referenced before assignment

我已经尝试过一切,我能做到的。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

了解如何为msg分配值:

if last == 0 or msg1 == 0:
    msg = '{0} some useless tex'.format(msg1)
if msg1 == 1 or (last == 1 and msg1 >= 21):
    msg = '{0} some useless tex'.format(msg1)
if (msg1 >= 2 and msg1 <= 4) or (msg1 >= 22 and msg1 <= 24):
    msg = '{0} some useless tex'.format(msg1)
if (last >= 5 and last <= 9):
    msg = '{0} some useless tex'.format(msg1)

那么,如果msg1是10-20范围内的某个值(并且最后不是0)会发生什么?那么这些if都不会触发。所以msg将永远不会被分配任何东西。但无论如何你都会尝试使用它,并得到那个错误。

你可能想要这样的东西:

if last == 0 or msg1 == 0:
    msg = '{0} some useless tex'.format(msg1)
elif msg1 == 1 or (last == 1 and msg1 >= 21):
    msg = '{0} some useless tex'.format(msg1)
elif (msg1 >= 2 and msg1 <= 4) or (msg1 >= 22 and msg1 <= 24):
    msg = '{0} some useless tex'.format(msg1)
elif (last >= 5 and last <= 9):
    msg = '{0} some useless tex'.format(msg1)
else:
    msg = '{0} WARNING! UNEXPECTED VALUE!'.format(msg1)

(请注意,将所有if更改为elif可能是一个语义上有意义的更改。如果两个条件都可以为true,则原始代码将同时执行它们,因此第二个会覆盖第一个做的,但是新代码只执行第一个。如果它有所不同,这实际上更常见于你想要的 - 但如果你想要其他行为,你可能需要重写或重新排序你的条件。)