Python Forum
Problem with 'and' in 'if' statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with 'and' in 'if' statement
#1
Hello,

I'm trying to make a function that returns true if a list contains at least one of many predetermined letters in it. The code is as follows:

def check(x):
    if 'A' and 'B' and 'C' in x:
        return True
    elif 'A' and 'D' and 'G' in x:
        return True
    elif 'A' and 'E' and 'I' in x:
        return True
    elif 'I' and 'H' and 'G' in x:
        return True
    elif 'I' and 'F' and 'C' in x:
        return True
    elif 'D' and 'E' and 'F' in x:
        return True
    elif 'B' and 'E' and 'H' in x:
        return True
    else:
        return False
L = ['A', 'B', 'E', 'F']

print(check(L))
In this instance it should return false, since there are none of the series of three letters that I'm checking for, but it somehow returns true...

Am I using 'and' wrong?
Reply
#2
Yes, you are using and wrong
Look at https://docs.python.org/3/library/stdtyp...ue-testing

'A' and 'B' and 'C' in x is same as True and True and 'C' in x
def check(iterable):
    sequences = ('ABC', 'ADG', 'AEI', 'IHG', 'IFC', 'DEF', 'BEH')
    return any(all(char in iterable for char in seq) for seq in sequences)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-02-2019, 10:20 AM)buran Wrote: Yes, you are using and wrong
Look at https://docs.python.org/3/library/stdtyp...ue-testing

'A' and 'B' and 'C' in x is same as True and True and 'C' in x
def check(iterable):
    sequences = ('ABC', 'ADG', 'AEI', 'IHG', 'IFC', 'DEF', 'BEH')
    return any(all(char in iterable for char in seq) for seq in sequences)

Oh I see. Thank you. So would it work if instead of A and B and C in x I write A in x and B in x and C in x?
Reply
#4
(Oct-06-2019, 07:12 PM)CoderMan Wrote: So would it work if instead of A and B and C in x I write A in x and B in x and C in x?
yes it will work. But you will have a lot of repeating code. My example turns your 17 lines of code into 3 lines
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 1,621 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 2,387 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,540 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,738 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 2,535 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 2,393 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 4,888 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,152 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,224 May-20-2019, 09:26 AM
Last Post: avorane
  if statement and in operator problem bobger 5 3,945 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020