How to check all the conditions with 1 AND operator

Question:

I want to check 2 conditions with just 1 AND operator.

I tried the following:

JavaScript:

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

With adjusted parentheses:

    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

With adjusted parentheses:

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

[ console log false ]

My original Python code:

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

With adjusted parentheses:


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

||

Answers:

First off you need to capitalize true and false to True and False. Then it seems like you have problems with your logic. You could write a simple test to verify your code:

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

For the code above the output would be:

([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)

With this simple code its easy to play with the logic and get a feel for where to put parenthesis and operators. Have fun!

Answered By: UlfR

Change your if statement to this.

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

Answered By: codester_09