Home > Mobile >  How to check all the conditions with 1 AND operator
How to check all the conditions with 1 AND operator

Time:09-29

I wanna check 2 conditions with just 1 "AND" operator {solved}

what i tried:

JS: {solved}

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

PARENTESIS ADJUST:

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

[ console log false ]

PY: {solved}

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

PARENTESIS ADJUST:

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

[ console log false ]

OBS:
im creating a bot to discord in discord.py, then if you can help me with the PY version...

this was my 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')

PARENTESIS ADJUST:


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

CodePudding user response:

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

CodePudding user response:

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!

  • Related