问题描述
非常简短的版本:我的elif
语句应该检查是否存在子字符串,它说在存在子字符串时不存在。
长版:我正在分解一个列表以提取有用的信息。由于列表是从用户派生的,因此我想检查列表是否正确填写,然后再继续。但是,当我尝试运行一些基本检查(例如是否存在正确的字符串)时,它会返回一条消息,告诉我列表项不符合我的要求。
我已经检查过,并且我的示例数据实际上已正确填写。但是,我正在努力找出我的检查失败的原因。
下面是一个复制我的问题的示例:
#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")
这将返回以下错误...
'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)
我还尝试了一个小的更改,在其中我将elif语句的条件设置为变量:
#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")
但这只是给出了稍微不同的信息...
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)
解决方法
您有陈述并没有按照您认为的那样做。
elif ('omit' not in thingy) or ('include' not in thingy):
唯一不正确的方法是,如果omit
和include
都在thingy
中,则在任何情况下都不是这样。
其他不符合您的意思的东西:
obligatory_characters = (' if: ' and ')' and '(')
print(obligatory_characters)
# '('
如果要检查字符串中是否存在几个不同的字符串,则需要执行类似的操作
must_exist = ['a','b','c']
for to_match in must_exist:
if to_match not in search_string:
return False
return True