英文:
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 False
和return 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论