首页 > 解决方案 > 这里有重复“或”语句(python)的替代方法吗?

问题描述

所以这里似乎有一个问题,当使用: 时if message.content.startswith((prefix + smalltalk0[0]) or (prefix + smalltalk0[1]) or (prefix + smalltalk0[2])):,if 语句似乎只适用于第一个条件: (prefix + smalltalk0[0]), (我写“sab hi”,它会响应。但它不响应“sab hello ”或“sab hey”出于某种原因。)

有人可以在这里帮助我吗,我猜我的错误与使用多个 or 语句有关,但更正其他任何可能错误的地方,非常感谢!

这是我所有的代码(是的,我确实知道帮助命令还没有完成并且可以正常工作,哈哈,我只是对位于代码末尾的闲聊区感到困扰!):

import discord
import random
import asyncio

TOKEN = '*insert token here'

client = discord.Client()

prefix = ("sab ")



@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith(prefix + "help"):
        _help = "{0.author.mention}...Check your PMs".format(message)
        await message.channel.send("Prefix: 'sab '(yes, including the space after)\nGENERAL COMMANDS\n------------\n'help': Sends some stuff on how to use the bot.\n'tell a joke': Tells a rubbish joke...".format(author))
        await message.channel.send(_help)
    if message.content.startswith(prefix + "tell a joke"):



        joke = ["What did the grape do when he got stepped on?",
                "Why couldnt the bicycle stand up by itself?",
                "How did the frog die?",
                "What do you call a can opener that doesn't work?",
                "Why does he want his job to be cleaning mirrors?",
                "What does a clock do when it's hungry?",
                "Why do sea-gulls fly over the sea?"]

        answer = ["He let out a little wine!",
                  "Because it's two-tired!",
                  "He Kermit suicide!",
                  "A can't opener!",
                  "Because it's something he can really see himself doing!",
                  "It goes back four seconds!",
                  "Because if they flew over the bay they would be bagels!"]

        y = [0, 1, 2, 3, 4, 5, 6]
        x = random.choice(y)
        jokenum = joke[x]
        answernum = answer[x]
        msg = jokenum.format(message)
        msg2 = answernum.format(message)
        await asyncio.sleep(1)
        await message.channel.send(msg)
        await asyncio.sleep(4)
        await message.channel.send(msg2)

    colours = ["blue", "orange", "yellow", "red", "green" ]
    p = [0, 1, 2, 3, 4] 
    z = random.choice(p)
    colournum = colours[z]
    colourcorrect = str(colournum)
    if message.content.startswith(prefix + "play eye spy"):
        await message.channel.send("Eye spy with my little eyes, a certain colour!(Guess it!)")
    if colourcorrect in message.content:
        await message.channel.send("Correct, " + colourcorrect + "!")







    smalltalk0 = ["hi", "hello", "hey"]
    q = [0, 1, 2]
    s = random.choice(q)
    smalltalk = smalltalk0[s]
    if message.content.startswith((prefix + smalltalk0[0]) or (prefix + smalltalk0[1]) or (prefix + smalltalk0[2])):
        await message.channel.send(smalltalk)

@client.event
async def on_ready():
    print('Sabios ready')
client.run(TOKEN)

标签: pythonarrayspython-3.xconditional-statementsdiscord.py

解决方案


首先,这不是or工作方式。您实际上需要重复startswith()调​​用并将不同的调用分开or

if (
    message.content.startswith(prefix + smalltalk0[0])
    or message.content.startswith(prefix + smalltalk0[1])
    or message.content.startswith(prefix + smalltalk0[2])
):
    await message.channel.send(smalltalk)

但是您确实可以使用以下方法简化它any

if any(message.content.startswith(prefix + x) for x in smalltalk0):
    await message.channel.send(smalltalk)

或利用startswith()接受元组的事实来检查:

if message.content.startswith(tuple(prefix + x for x in smalltalk0)):
    await message.channel.send(smalltalk)

但是,此函数中的所有检查都使用前缀。那么为什么不在一开始就做这样的事情呢?

if message.author == client.user:
    return
if not message.content.startswith(prefix):
    return
content = message.content[len(prefix):]

像这样,您content在前缀之后只包含消息,然后可以简单地检查例如:

if message.content.startswith(tuple(smalltalk0)):
    ...

推荐阅读