Python 3 if/elif/else 运算符总是转到 else

标签 python discord.py

我正在使用 Python 3.5 编写一个不和谐的机器人,我遇到了一个问题,但我似乎无法解决它,这是代码

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')

基本上,这里发生的情况是,如果用户输入“!juvialist add”,Python 在 add 之前对所有内容进行切片,并将其放入名为“decision”的字符串中,然后 IF 语句决定会发生什么,如果是添加或删除,则执行某些操作,否则它告诉用户参数错误,但问题是,即使正确,它总是转到else并返回错误

错误:

Image

我尝试过做同样的事情,但基于谷歌搜索的方式略有不同,查看有关 IF 语句的 Python 文档也没有多大帮助,这可能意味着我错过了一些非常明显的东西,所以我'如果您能在这里帮助我,我将不胜感激,谢谢。

最佳答案

因为你错过了空间。事实上,您的决定“添加”或其他。

改为使用decision = message.content[11:]

或者更好的想法是decision = message.content[10:].strip()

关于Python 3 if/elif/else 运算符总是转到 else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580520/

相关文章:

python - discord.py 重写 : TypeError: cogs must derive from Cog

python - 如何解封用户?

python - 不和谐 py 中的 @client.command() 对我不起作用

python - 如何搜索字符串、提取其中的一部分并将其用作变量

python - 在Python中打印类中包含的所有元素

python - 如何使用 django paginator 在页面上拆分 DataFrame (pandas)?

python:打印十六进制数据而不是字典

discord - 检查特定用户是否使用reactdiscord.py

Python 无法调用函数

python - Discord.py 重启命令