嵌套Discord.Py的If和Else语句

时间:2018-08-30 23:20:44

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

我一直在努力使用此命令一段时间。我可以拿一张支票上班,但不能拿另一张。我想确保用户不会提及自己,并且如果他们从get_dollars提取的钱少于0美元,那么他们就不能“抢劫”另一个用户。我已经尝试过"try, except, else, finally"并将其投入半工,但它会吐出两条消息,而不是一条消息。然后,我尝试下面的代码,以考虑是否嵌套ifelse语句会更好地工作。这样会遇到if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment错误。

if message.content.startswith('!rob'):
        if message.author == user.mention:
            await client.send_message(message.channel, "{} you cant rob yourself! ".format(message.author.mention))
        else:
            if get_dollars(message.author) < 0:
                await client.send_message(message.channel, "{} you can't even afford a gun.".format(message.author.mention))
            else:
                for user in message.mentions:
                    if (get_dollars(user)) < 1:
                        await client.send_message(message.channel, "{} is too broke to rob ".format(user.mention))
                    elif steal <= 3:
                        print("{} catches {} slippin and sticks them up for ${} ".format(message.author.mention, user.mention, stealdollars))
                        await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} ".format(message.author.mention, user.mention, stealdollars))
                        remove_dollars(user, stealdollars)
                        add_dollars(message.author, stealdollars)
                    elif steal == 4:
                        print("{} gets arrested trying to rob {} and has to post $250 bail ".format(message.author.mention, user.mention))
                        await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail ".format(message.author.mention, user.mention))
                        remove_dollars(message.author, 250)
                    elif steal >= 5:
                        print("{} kicks {}'s door down but doesn't find any cash ".format(message.author.mention, user.mention))
                        await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash ".format(message.author.mention, user.mention))

1 个答案:

答案 0 :(得分:0)

就像Patrick所说,没有用户定义。最终将user = message.mentions [0]放进去,命令运行正常。