简体   繁体   中英

`elif` statement failing to a check string for a substring

Very short version: my elif statement that is supposed to check if a substring is present, is saying that a substring isn't present when it is.

Longer version: I'm breaking down a list to extract useful information. As the list is derived from a user I want to check it's been filled out correctly before I proceed. However, when I try to run some basic checks (eg are the correct strings present) it returns a message telling me the list items do not meet the specification I require.

I have checked and my example data is actually filled-in correctly. However, I'm struggling to work out why my check fails.

An example that replicates my problem is given below:

    #Example of a string I am turning into a list
meta_thingys_str = "TagExampleA(omit if: Data Example 1), TagExampleA(include if: Data Example 2), TagExampleB(include if: any), TagExampleC(include if: any), TagExampleC(omit if: Data Example 3)"

#Split the string based on ', ' into a list
meta_thingys = meta_thingys_str.split(", ")

#Useful for later, to see if the list items are laid out in a manner my code can read.
obligatory_characters = (' if: ' and ')' and '(') 


#Let's loop through our "thingys"
for thingy in meta_thingys:

    #A silly example - I have a lot more if-statements in my actual code but
    #Obviously `elif` won't run without an `if` statement.
    if ("include" in thingy) and ("any" not in thingy):
        print('yay')

    #This checks to see if the layout and characters of the list items are 
    #roughly correct
    elif (obligatory_characters not in thingy):
        print("'" + thingy + "' not recognised as a valid input (Type A Error)\n")

    #This is *supposed to* check whether either the key word 'omit' or 'include'
    #is present within each list item. Seemingly, it fails.
    elif ('omit' not in thingy) or ('include' not in thingy):
        print("\n'" + thingy + "' not recognised as a valid input (Type B Error)\n")

This returns the following error...

'TagExampleA(omit if: Data Example 1)' not recognised as a valid input (Type B Error)

yay

'TagExampleB(include if: any)' not recognised as a valid input (Type B Error)


'TagExampleC(include if: any)' not recognised as a valid input (Type B Error)


'TagExampleC(omit if: Data Example 3)' not recognised as a valid input (Type B Error)

I also tried a small change, where I set as a variable the condition for the elif statement:

#Useful for later, to see if the list (derived from a user input) contains the
#proper information
obligatory_characters = (' if: ' and ')' and '(')
#!>>> note the change here <<<!
variable_characters = ('omit' or 'include')

#Let's loop through our "thingys"
for thingy in meta_thingys:
#A silly example I have a lot more if-statements in my actual code
if ("include" in thingy) and ("any" not in thingy):
    print('yay')

#This checks to see if the layout and characters of the list items are
#roughly correct
elif (obligatory_characters not in thingy):
    print("'" + thingy + "' not recognised as a valid input (Type A Error)\n")

#This is *supposed to* check whether either the key word 'omit' or 'include'
#is present within each list item. Seemingly, it fails.
#!>>> and corresponding change here <<<!
elif (variable_characters not in thingy):
    print("\n'" + thingy + "' not recognised as a valid input (Type B Error)\n") 

This however just gives a slightly different message...

yay

'TagExampleB(include if: any)' not recognised as a valid input (Type B Error)


'TagExampleC(include if: any)' not recognised as a valid input (Type B Error)

You've got statements that aren't doing what you think they're doing.

elif ('omit' not in thingy) or ('include' not in thingy):

The only way this isn't true is if both omit and include are in thingy , which isn't true in any of your cases.

Other things that don't mean what you think they mean:

obligatory_characters = (' if: ' and ')' and '(')
print(obligatory_characters)
# '('

If you want to check that several different strings exist in a string, you need to do something like

must_exist = ['a', 'b', 'c']
for to_match in must_exist:
    if to_match not in search_string:
        return False
return True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM