为什么输出显示为false,而应该是true?

huangapple go评论91阅读模式
英文:

why the output print false while it should be true?

问题

def match_the_words(pattern):

    the_words = ["hello", "shello", "qwello", "jello", "jelly", "jelloy", "hellboy"]
    for word in the_words:
        if not re.fullmatch(pattern, word):
            return False
    return True

print(match_the_words("hello"))

print(match_the_words("hell"))
英文:

I have to write a code use regulare expression. the outputs print both false while it should be true and false.

def match_the_words(pattern):

    the_words = ["hello", "shello", "qwello", "jello", "jelly", "jelloy", "hellboy"]
    for word in the_words:
        if not re.fullmatch(pattern, word):
            return False
    return True

print(match_the_words("hello"))

print(match_the_words("hell"))

答案1

得分: 1

如果其中任何一个词不匹配该函数,则返回false,因为return false位于循环和否定条件中,所以如果条件一次为false,它就会返回false。
反转return Falsereturn True
通常,您应该尝试调试,因为您会更好地理解它,更重要的是自己,这总是一个好习惯。

英文:

If any of the words dont match the function return false because the return false is in the loop and the negative condition, so if the condition is false once it returns false.<br> Reverse the return False and return True.<br>
In general you should try debugging because you would've understood it better and more importantly by yourself, which is always good to do.

huangapple
  • 本文由 发表于 2023年7月23日 18:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定