【问题标题】:How to check all the conditions with 1 AND operator [closed]如何使用 1 AND 运算符检查所有条件 [关闭]
【发布时间】:2022-10-04 20:18:29
【问题描述】:

我想用 1 个 AND 运算符检查 2 个条件。

我尝试了以下方法:

JavaScript:

    a = True
    b = False
    x = 3
    
    if (x == 2 & a == true | b == false){
      return;
    }

带调整括号:

    a = True
    b = False
    x = 3
    
    if (x == 2 & (a == true | b == false)){
      return;
    }

[ console log false ]

Python:

    a = True
    b = False
    x = 3
    
    if x == 2 & a == true or b == false:
      return

带调整括号:

    a = True
    b = False
    x = 3
    
    if x == 2 & (a == true or b == false):
      return

[ console log false ]

我的原始 Python 代码:

import discord

intents = discord.Intents.default()
intents.messages = True
intents.message_content = True

client = discord.Client(intents=intents)

nomes = ['filho', 'franguinho', 'franguinho jr', 'frango jr'] # NAMES OF THE BOT

@client.event
async def on_ready():
    print('Logged')
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="ACTIVITY"))

async def gsend(msg):
    client.send_message(client.get_channel("ID OF CHANNEL"), msg)

@client.event
async def on_message(message):
    
    mensagem = message.content.lower()
    pessoa = message.author
    pai = 'ID OF USER'

    if pessoa.id == pai and mensagem in nomes or mensagem in "<@"+str(client.user.id)+">":
        await message.channel.send('MESSAGE TO TALK')




client.run('TOKEN OF DISCORD BOT')

带调整括号:


if pessoa.id == pai and (mensagem in nomes or mensagem in "<@"+str(client.user.id)+">"):
        await message.channel.send('MESSAGE TO TALK')

【问题讨论】:

  • 在 python 中,&amp; 是一个明智的运算符,使用 and 代替它。
  • 我已经试过了,还是不行
  • 不要使用; &和python中的小写true或false
  • 我们当然不是来教你语言本身的,最好看the Python docs for operators并提出具体问题
  • 我们当然是来教我的,这是一个论坛

标签: python discord.py operators


【解决方案1】:

将您的 if 语句更改为此。

if pessoa.id == pai and (mensagem in nomes or mensagem in "<@"+str(client.user.id)+">"):
        await message.channel.send('MESSAGE TO TALK')

【讨论】:

    【解决方案2】:

    首先,您需要将真假大写为TrueFalse。那么你的逻辑似乎有问题。您可以编写一个简单的测试来验证您的代码:

    # Generate data to with all possible combinations
    data = [(a,b,x) for a in [False,True] for b in [False, True] for x in [2,3]]
    # Print all combinations and the result of the expression
    for (a,b,x) in data: print([a,b,x], x == 2 and a == True or b == False)
    

    对于上面的代码,输出将是:

    ([False, False, 2], True)
    ([False, False, 3], True)
    ([False, True, 2], False)
    ([False, True, 3], False)
    ([True, False, 2], True)
    ([True, False, 3], True)
    ([True, True, 2], True)
    ([True, True, 3], False)
    

    使用这个简单的代码,可以轻松地使用逻辑并了解括号和运算符的放置位置。玩得开心!

    【讨论】:

    • 谢谢,你的回答也有帮助,但我真的不记得括号了,sry
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2013-01-22
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    相关资源
    最近更新 更多