Python Discord bot: Conditional statement issue with team assignment

Hey everyone! I’m new to coding and I’m working on a Discord bot. I’m trying to make it so users can input between 2 and 10 teams for a check-in system. The bot works fine with 10 teams, but when fewer teams are entered, I get an error saying ‘NoneType’ object has no attribute ‘name’.

I think I know why it’s happening. When fewer than 10 teams are provided, some team variables end up as None and therefore lack a name attribute. However, I’m puzzled why the code still attempts to access the name attribute even when the condition should skip it.

Below is a simplified version of my code:

@bot.command()
async def set_teams(ctx, *teams):
    team_names = []
    for team in teams[:10]:
        if team != 'None':
            team_names.append(team.name)
    
    await ctx.send(f'{len(team_names)} teams added: {team_names}')

I’ve experimented with adjusting the indentation and even using a pass statement, but the error persists. Can someone please help me understand what’s causing this issue? Thanks!

hey man, looks like ur mixing up None and ‘None’. try changing ur if statement to if team is not None: instead. that should fix it. also, make sure the teams ur passing are actually objects with a name attribute, not just strings. good luck!

I’ve encountered a similar issue in my Discord bot development. The problem lies in how you’re checking for None values. Instead of using !=, try using ‘is not None’. This approach checks for actual None objects rather than the string ‘None’.

Also, ensure that the teams you’re passing are objects that actually have a ‘name’ attribute. If they’re just strings, you’ll need to adjust your code to handle that accordingly. Here’s a quick fix:

@bot.command()
async def set_teams(ctx, *teams):
    team_names = [team.name for team in teams[:10] if team is not None]
    await ctx.send(f'{len(team_names)} teams added: {team_names}')

This modification should resolve your issue. Additionally, consider implementing error handling for unexpected inputs to make your bot more robust.