Python Forum
Combine Or and And in If Statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combine Or and And in If Statement
#6
I'm just an amateur, but I thought this might help on a different track:

import random # just for making the strings

# list of things we want, add what you like or generate it 
wanted = ['AH', 'MH', 'GL', 'ML']
# list of things we need to check, add what you like or generate it 
check_list = ['GL']
# list of things we do not want in combination with check_list, add what you like or generate it 
not_wanted_in_comboGL = ['ML']
# if for example you also don't want AH in combo with say MH
# not_wanted_in_comboAH = ['MH']
# you need another if in lookup(alist)

# make a random test set as a substitute for mStrip

def makeTestset():
    test_set = []
    while len(test_set) < 10:
        randomUpperLetter1 = chr(random.randint(ord('A'), ord('M')))
        randomUpperLetter2 = chr(random.randint(ord('A'), ord('M')))
        string = randomUpperLetter1 + randomUpperLetter2
        if string in wanted: 
            test_set.append(string)
            print(string)
    return test_set

def lookup(mylist):
    # maybe there are no unwanted combinations
    for x in mylist:
        if x in wanted:            
            result = 'OK'
            print(x, result)        
        if x in check_list:
            # check if an unwanted combination is there
            for y in mylist:
                if y in not_wanted_in_comboGL:
                    result = 'not OK'
                    print(x, y, result)
                    # break if an unwanted combination is found
                    return result
    return result
    
alist = makeTestset()
answer = lookup(alist)
print(answer)
JamesA likes this post
Reply


Messages In This Thread
Combine Or and And in If Statement - by JamesA - Jul-22-2021, 10:18 AM
RE: Combine Or and And in If Statement - by Pedroski55 - Jul-22-2021, 11:58 PM
RE: Combine Or and And in If Statement - by JamesA - Jul-24-2021, 12:26 PM

Forum Jump:

User Panel Messages

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