#If statement (unexpectedly) returns all.

1 messages · Page 1 of 1 (latest)

tiny rose
#

My tag command is only supposed to return a specified tag, but instead it returns all.

#

Here is my code:

#
@bot.command(guild_ids=[1007844136738619545])
async def tag(ctx, tag):
    if tag == 'codeblock':
        await ctx.respond("Please place your code in codeblocks. \`\`\` your code here \`\`\` Your code would appear as this: \`\`\`your code here \`\`\`")
    if tag == 'didnotwork' or 'didntwork':
        await ctx.respond("Just saying that it \"didn\'t work\" isn\'t helpful. Please give us the code in question, and a valid error code. Censor your token!")
    if tag == 'tags':
        await ctx.respond("The tags available are: codeblock, didnotwork, didntwork, reports, report, tryitandsee, tryitands.ee", ephemeral=True)
    if tag == 'report' or 'reports':
        await ctx.respond("Please report your issues using /report.")
    if tag == 'tryitandsee' or 'tryitands.ee':
        await ctx.respond("https://tryitands.ee")
    else:
        await ctx.respond(f"I\'m afraid I can\'t reference tags... that... you know... don\'t exist.",ephemeral=True)
#

If statement (unexpectedly) returns all.

fervent gulch
#

can you post the bot response and command input?

tiny rose
#

/tag tag:tags

fervent gulch
#

ah, that's why

#

because you have all of your statements as if instead of elif, it does all of them separately (excluding the last two, which are done together)

tiny rose
#

oh ok

fervent gulch
#

the reason it gives responses 2, 4, and 5, is because strings are always true ('didntwork' is not being compared to tag but is being evaluated to True)

shadow cairn
#

use match case 🙂

fervent gulch
#

one way you could do it is

@bot.command(name='tag')
async def tag(ctx, tag):
    if tag == 'codeblock':
        await ctx.respond("Please place your code in codeblocks. \`\`\` your code here \`\`\` Your code would appear as this: \`\`\`your code here \`\`\`")
    elif tag in ['didnotwork', 'didntwork']:
        await ctx.respond("Just saying that it \"didn\'t work\" isn\'t helpful. Please give us the code in question, and a valid error code. Censor your token!")
    elif tag == 'tags':
        await ctx.respond("The tags available are: codeblock, didnotwork, didntwork, reports, report, tryitandsee, tryitands.ee", ephemeral=True)
    elif tag in ['report', 'reports']:
        await ctx.respond("Please report your issues using /report.")
    elif tag in ['tryitandsee', 'tryitands.ee']:
        await ctx.respond("https://tryitands.ee")
    else:
        await ctx.respond(f"I\'m afraid I can\'t reference tags... that... you know... don\'t exist.",ephemeral=True)
tiny rose
#

elif doesn't exist on mine?

fervent gulch
#

are you sure? it's been in python since forever

#

or use match case, that would probably also be more readable

tiny rose
#

yep it throws a fit

fervent gulch
#

hover it and screenshot the error for me

#

as in mouseover

shadow cairn
#

lol

tiny rose
shadow cairn
#

my guy you really need to take a lesson holy

#

'elif' as your first argument

fervent gulch
#

oh i didn't even notice lol

shadow cairn
#

you need at least if to start with

fervent gulch
#

yeah first is always if, following ones can be elif or else, but else is always last (if there is an else at all)

tiny rose
#

Now all the tags are the first one 😅

tiny rose
#

the first elif i mean

fervent gulch
#

because the string 'didntwork' is being evaluated as just a string and not being compared to tag

tiny rose
#

but how would i do it

#

so should i do if 'codeblock' == tag instead

#

?

fervent gulch
#

or

if tag == 'didnotwork' or tag == 'didntwork':
    # etc.
tiny rose
#

My PC died in the middle of coding lol

#

ooh its back

shadow cairn
#
@bot.command(name='tag')
async def tag(ctx, tag):
    match str(tag):
        case 'codeblock':
            await ctx.respond("Please place your code in codeblocks. \`\`\` your code here \`\`\` Your code would appear as this: \`\`\`your code here \`\`\`")
        case 'didnotwork' | 'didntwork':
            await ctx.respond("Just saying that it \"didn\'t work\" isn\'t helpful. Please give us the code in question, and a valid error code. Censor your token!")
        case 'tags':
            await ctx.respond("The tags available are: codeblock, didnotwork, didntwork, reports, report, tryitandsee, tryitands.ee", ephemeral=True)
        case 'report' | 'reports':
            await ctx.respond("Please report your issues using /report.")
        case 'tryitandsee' | 'tryitands.ee':
            await ctx.respond("https://tryitands.ee")
        case _:
            await ctx.respond(f"I\'m afraid I can\'t reference tags... that... you know... don\'t exist.",ephemeral=True)
tiny rose
#

Closing post now @fervent gulch