简体   繁体   English

Python:AttributeError:'NoneType'对象没有属性'append'

[英]Python : AttributeError: 'NoneType' object has no attribute 'append'

My program looks like我的程序看起来像

# global
item_to_bucket_list_map = {}

def fill_item_bucket_map(items, buckets):
    global item_to_bucket_list_map

    for i in range(1, items + 1):
        j = 1
        while i * j <= buckets:
            if j == 1:
                item_to_bucket_list_map[i] = [j]
            else:
                item_to_bucket_list_map[i] = (item_to_bucket_list_map.get(i)).append(j)
            j += 1
        print "Item=%s, bucket=%s" % (i, item_to_bucket_list_map.get(i))


if __name__ == "__main__":
    buckets = 100
    items = 100
    fill_item_bucket_map(items, buckets)

When I run this, it throws me当我运行它时,它会抛出我

AttributeError: 'NoneType' object has no attribute 'append'

Not sure why this would happen?不知道为什么会发生这种情况? When I am already creating a list at start of each j当我已经在每个j的开头创建一个列表时

Actually you stored None here: append() changes the list in place and returns None 实际上,您在此处None存储Noneappend()将列表更改到位并返回None

 item_to_bucket_list_map[i] = (item_to_bucket_list_map.get(i)).append(j)

example: 例:

In [42]: lis = [1,2,3]

In [43]: print lis.append(4)
None

In [44]: lis
Out[44]: [1, 2, 3, 4]
[...]
for i in range(1, items + 1):
    j = 1
    while i * j <= buckets:
        if j == 1:
            mylist = []
        else:
            mylist = item_to_bucket_list_map.get(i)
        mylist.append(j)
        item_to_bucket_list_map[i] = mylist
        j += 1
    print "Item=%s, bucket=%s" % (i, item_to_bucket_list_map.get(i))

The while loop, however, can be simplified to 然而, while循环可以简化为

    for j in range(1, buckets / i + 1): # + 1 due to the <=
        if j == 1:
            mylist = []
        else:
            mylist = item_to_bucket_list_map.get(i)
        mylist.append(j)
        item_to_bucket_list_map[i] = mylist

AttributeError always comes here: 'NoneType' object has no attribute 'categories' can someone help me? AttributeError 总是出现在这里:'NoneType' 对象没有属性 'categories' 有人可以帮我吗?

import discord
from discord.ext import commands


from discord_slash import SlashCommand
from discord_slash.model import ButtonStyle
from discord_slash.utils.manage_components import (
    ComponentContext,
    create_actionrow,
    create_button,
)

client = commands.Bot(command_prefix="-", case_insensitive=True, help_command=None)

slash = SlashCommand(client)


TICKET_MOD_ROLE_ID = '915020370673799188'
MANAGEMENT_ROLE_ID = '915020370673799188'
GUILD_ID = '908012453227552848'

ticket_category = None
ticket_mod_role = None
management_role = None
guild = None


@client.event
async def on_ready():
    print("Bot is ready")

    global ticket_category, ticket_mod_role, management_role


    guild = client.get_guild(GUILD_ID)


    ticket_category = discord.utils.get(guild.categories, name="ticket")

    ticket_mod_role = guild.get_role(
        role_id=TICKET_MOD_ROLE_ID
    )
    management_role = guild.get_role(role_id=MANAGEMENT_ROLE_ID)


@client.event
async def on_component(ctx: ComponentContext):
    await ctx.defer(
        ignore=True
    )

    ticket_created_embed = discord.Embed(
        title="Ticket Processed",
        description=f"""Hey {ctx.author.name}! Thanks for opening a ticket with us today, but before we transfer you through to a manager, we have to approve your ticket. We have this step in place to prevent bots and spam tickets.
        Please describe your enquiry and our team will approve it shortly. We thank you in advance for your patience.""",
    )
[20:54]
overwrites = {
        guild.default_role: discord.PermissionOverwrite(view_channel=False),
        guild.me: discord.PermissionOverwrite(view_channel=True),
        ticket_mod_role: discord.PermissionOverwrite(view_channel=True),
    }

    ticket = await ticket_category.create_text_channel(
        f"{ctx.author.name}-{ctx.author.discriminator}", overwrites=overwrites
    )

    await ticket.send(
        ctx.author.mention, embed=ticket_created_embed
    )


@client.command()
@commands.has_role(TICKET_MOD_ROLE_ID)
async def sendticket(ctx):
    embed = discord.Embed(
        title="Contact Support",
        description="Click the button below to open a ticket",
    )

    actionrow = create_actionrow(
        *[
            create_button(
                label="Open Ticket", custom_id="ticket", style=ButtonStyle.primary
            ),
        ]
    )

    await ctx.send(embed=embed, components=[actionrow])


@client.command(aliases=["approve"])
@commands.has_role(TICKET_MOD_ROLE_ID)
async def up(ctx):
    overwrites = {
        ctx.guild.me: discord.PermissionOverwrite(view_channel=True),
        ctx.guild.default_role: discord.PermissionOverwrite(view_channel=False),
        ticket_mod_role: discord.PermissionOverwrite(view_channel=None),
        management_role: discord.PermissionOverwrite(view_channel=True),
    }
    await ctx.channel.edit(overwrites=overwrites)

    await ctx.channel.send(
        "Ticket Approved!\nYour ticket has been approved and has been transferred through to the Management Team. They will assist you further with your enquiry."
    )


@client.command()
@commands.has_role(TICKET_MOD_ROLE_ID)
async def close(ctx):
    await ctx.channel.delete()



client.run("........")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;NoneType&#39;对象在edX上的Python中没有属性&#39;append&#39; - AttributeError: 'NoneType' object has no attribute 'append' in Python on edX AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - AttributeError: 'NoneType' object has no attribute 'append' 错误“ AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39;” - Error “AttributeError: 'NoneType' object has no attribute 'append'” 将列表附加到列表:AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - Append a list to a list: AttributeError: 'NoneType' object has no attribute 'append' AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;findAll&#39; - Python - AttributeError: 'NoneType' object has no attribute 'findAll' - Python Python:AttributeError:'NoneType'对象没有属性'rfind' - Python: AttributeError: 'NoneType' object has no attribute 'rfind' AttributeError:“ NoneType”对象在python中没有属性“ read” - AttributeError: 'NoneType' object has no attribute 'read' in python AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;getText&#39; (Python) - AttributeError: 'NoneType' object has no attribute 'getText' (Python) AttributeError:“ NoneType”对象在python中没有属性“ text” - AttributeError: 'NoneType' object has no attribute 'text' in python AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;lower&#39; - AttributeError: 'NoneType' object has no attribute 'lower' in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM