Myronaz
Myronaz

Reputation: 173

Python 3 if/elif/else operator always goes to else

I'm writing a discord bot using Python 3.5 and i've run into an issue and I can't seem to solve it, here is the code

if message.content.startswith('!juvialist'):
    decision = message.content[10:]
    if decision == 'add':
        await client.send_message(message.channel, 'Added')
    elif decision == 'remove':
        await client.send_message(message.channel, 'Removed')
    else:
        await client.send_message(message.channel, decision + ' is an invalid parameter')

basically, what happens here is if the user types '!juvialist add' python slices everything before add and puts into a string called decision, then an IF statement decides what happens, if it's an add or remove it does something, else it tells the user that the parameter is wrong, but the problem is, even if it's correct, it always goes to else and returns an error

Error:

Image

I've tried doing the same thing but in slightly different ways based on Google searches, looking at Python documentation on IF statements isn't much help either, which probably means that i'm missing something ridiculously obvious, so i'd appreciate it if you could help me here, thank you.

Upvotes: 0

Views: 192

Answers (2)

hd1
hd1

Reputation: 34657

Even better is to use in instead of ==:

if message.content.startswith('!juvialist'):
    if ' add ' in message.content:
        await client.send_message(message.channel, 'Added')
    elif ' remove ' in message.content:
        await client.send_message(message.channel, 'Removed')
    else:
        # etc.

Upvotes: -1

grapes
grapes

Reputation: 8636

Because you missed up space. In fact your decision is " add" or whatever.

Put decision = message.content[11:] instead.

Or even better idea would be decision = message.content[10:].strip()

Upvotes: 2

Related Questions