【问题标题】:`elif` statement failing to a check string for a substring`elif` 语句未能检查子字符串的字符串
【发布时间】:2020-08-17 22:26:29
【问题描述】:

非常简短的版本:我的 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)

【问题讨论】:

  • @superbrain 好吧,您可以只运行代码,没有人强迫您阅读长版本。在过去,我遗漏了信息,人们用实际上无济于事的替代解决方案做出了回应。试图找到平衡就是一切。
  • @superbrain 案例和要点,我认为我的第一个 elif 声明很好而且很实用,这就是我包含它的原因,因为它让我感到困惑。事实证明,情况并不好。
  • 如果需要,我可以修剪一行实际代码,如果您包含注释,则可以修剪两行。如果这给您带来困扰,我很抱歉。但是,您不正确;我非常严重地误解了我的陈述的行为。如果您不打算调查事情,您可能无能为力。
  • @superbrain 老实说,我什至没有想到这可能是问题所在。事实上,obligatory_characters = (' if: ' and ')' and '(') 在我介绍它之后似乎让事情变得更好,所以我认为我已经在那个特定的实验中取得了进展。感谢您抽出宝贵时间。

标签: python string for-loop if-statement substring


【解决方案1】:

你的陈述并没有按照你的想法去做。

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

如果omitinclude 都在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

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多