如何解决如何使用附加字符串扩展功能
我目前正在尝试解决一些初学者练习,但我被困在应该扩展现有函数并添加允许我忽略大小写的字符串的点上。我真的不知道我应该在函数中的哪个位置添加最后一个信息。我还应该找到出现单词的所有行('Heaven'、'all'、'you')。
这是我现在所拥有的:
def all_line_numbers(text,word):
"""
Returns the line numbers (beginning with 1) where the word appears in
:param text: Text in which the word is searched for
:param word: A word to search for
:return: List of line numbers
"""
lines = text.splitlines()
i = 0
for line in lines:
i = i + 1
if word in line:
return i
return []
它以:
#
print()
print("exercise")
words = ['Heaven','all','you']
for word in words:
num = all_line_numbers(it_rains,word)
if num:
print(f"The word {word} is in the following lines {num}.")
else:
print(f"The word {word} was not found!")
在此先非常感谢您!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。