首页 > 解决方案 > 你如何在 python 的不同行上引用一个字符串?

问题描述

我正在做一些代码 atm,它当前执行请求,然后转到另一个频道,并编辑添加该人请求内容的时间表,我目前正在努力制作它,以便您可以将其设置在不和谐服务器而不是我的当前系统(引用特定的不和谐通道进行测试) atm 星期一区域在我的代码中没有找到星期一的字符串等,我不知道要更改什么来引用它,我是一个在线用户-go learner coder 所以我的代码可能看起来很糟糕,但我希望我能得到一些帮助,以便我可以改进。

@bot.command(name='raid', pass_context=True)
async def raid(ctx, arg, arg1 = None, arg2 = None, arg3 = None,*, role_name = None):
    if arg == 'setup' :
        setupchannel = ctx.channel
        ctx.channel.purge(amount=1)
        await setupchannel.send("**=====** __**RAID SCHEDULE**__ **=====**")
        Mondaymsg = await setupchannel.send("__**Monday**__")
        Tuesdaymsg = await setupchannel.send("__**Tuesday**__")
        Wednesdaymsg = await setupchannel.send("__**Wednesday**__")
        Thursdaymsg = await setupchannel.send("__**Thursday**__")
        Fridaymsg = await setupchannel.send("__**Friday**__")
        Saturdaymsg = await setupchannel.send("__**Saturday**__")
        Sundaymsg = await setupchannel.send("__**Sunday**__")
    elif arg == 'add':
        if arg1 == 'Monday':
            Userping = ctx.author.mention
            Message = Mondaymsg()
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            messageContent = Mondaymsg.message.content
            if arg3 in str(Mondaymsg.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await Mondaymsg.edit(content=messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Tuesday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879568853045235773)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Wednesday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879569158092783667)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Thursday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879569232336125972)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Friday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879569295313600573)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Saturday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879569379363258438)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        elif arg1 == 'Sunday':
            channel = bot.get_channel(879538507905921125)
            Userping = ctx.author.mention
            role = discord.utils.find(
                lambda r: r.name == role_name, ctx.guild.roles)
            message = await channel.fetch_message(879569473688973403)
            messageContent = message.content
            if arg3 in str(message.content):
                emoji = '❌'
                await ctx.message.add_reaction(emoji)
                await ctx.channel.send(Userping + " Your selected time is already taken on this day, please select a different time")
            else:
                if role in ctx.guild.roles:
                    emoji = '✅'
                    await ctx.message.add_reaction(emoji)
                    await message.edit(content = messageContent + "\n" + Userping + " | " + f"{role.mention}" + " " + arg2 + " | " + arg3)
                else:
                    await ctx.channel.send(Userping + " " + "I'm sorry, you need to input the name of the role you are hosting for (Example: Spectre Platoon, Diablo Squad etc.)")
        else:
            await ctx.channel.send("I'm sorry, your day input was invalid, please put one of the following days:\n\n"
                         "*Monday*\n"
                         "*Tuesday*\n"
                         "*Wednesday*\n"
                         "*Thursday*\n"
                         "*Friday*\n"
                         "*Saturday*\n"
                         "*Sunday*"
                         )
    elif arg == 'clear':
        channel = bot.get_channel(879538507905921125)
        mondaymessage = await channel.fetch_message(879568766365736972)
        tuesdaymessage = await channel.fetch_message(879568853045235773)
        wednesdaymessage = await channel.fetch_message(879569158092783667)
        thursdaymessage = await channel.fetch_message(879569232336125972)
        fridaymessage = await channel.fetch_message(879569295313600573)
        saturdaymessage = await channel.fetch_message(879569379363258438)
        sundaymessage = await channel.fetch_message(879569473688973403)
        await mondaymessage.edit(content='**__Monday__**')
        await tuesdaymessage.edit(content='**__Tuesday__**')
        await wednesdaymessage.edit(content='**__Wednesday__**')
        await thursdaymessage.edit(content='**__Thursday__**')
        await fridaymessage.edit(content='**__Friday__**')
        await saturdaymessage.edit(content='**__Saturday__**')
        await sundaymessage.edit(content='**__Sunday__**')

标签: pythonstring

解决方案


看起来您在问为什么在第一个 IF 主体中定义的变量在其他 IF 主体中不可访问:

def func():
    if some_condition:
        var1 = 'blah'
        var2 = 'asdf'
    elseif other_condition:
        # trying to access var1 or var2 here will result in a syntax error

这是因为两个 IF 语句体的作用域不同

一个简单的选择是首先None在函数外部使用一些默认值(例如)定义它们,创建全局变量(在全局范围内):

var1 = None
var2 = None

def func():
    if some_condition:
        var1 = 'blah'
        var2 = 'asdf'
    elif other_condition:
        # now var1 and var2 are accessible here,
        # but you must check or make sure they've been set
        # to the values you want, meaning
        # you must ensure func() has been called earlier
        # with `some_condition` as true

但通常定义全局变量并不是一个好习惯。一种替代方法是让您的函数接受它修改的状态变量:

class State(object):
    def __init__(self):
        self.var1 = None
        self.var2 = None

def func(state):
    if some_condition:
        state.var1 = 'blah'
        state.var2 = 'asdf'
    elif other_condition:
        # can use state.var1 and state.var2

if __name__ == '__main__':
    state = State()
    # can now call func(state) to read or write state.var1 and state.var2

推荐阅读