#If statement (unexpectedly) returns all.
1 messages · Page 1 of 1 (latest)
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.
can you post the bot response and command input?
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)
oh ok
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)
use match case 🙂
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)
elif doesn't exist on mine?
are you sure? it's been in python since forever
or use match case, that would probably also be more readable
yep it throws a fit
lol
oh i didn't even notice lol
you need at least if to start with
yeah first is always if, following ones can be elif or else, but else is always last (if there is an else at all)
Now all the tags are the first one 😅
.
the first elif i mean
because the string 'didntwork' is being evaluated as just a string and not being compared to tag
.
or
if tag == 'didnotwork' or tag == 'didntwork':
# etc.
@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)
AYYYY IT WORKED!
Closing post now @fervent gulch