0

I am currently trying to solve some beginners exercises but I am stuck at the point where I am supposed to expand an existing function and also add the string that allows me to ignore upper and lower cases. I do really do not know where in the function I should be adding this last information. I am also supposed to find all the lines where the words appear ('Heaven', 'all', 'you').

Here is what I have now:

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 []

and it ends with:

#
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!")

Thank you very much in advance!

buddies
  • 23
  • 5
  • Use the `lower()` function: `if word in line.lower():` – Barmar Mar 20 '21 at 06:38
  • You can use `.lower()` on `word` and `line` to match them ignoring case. – Abhi_J Mar 20 '21 at 06:39
  • thank you, it helped me out! The only problem is, I do get only one result. Meaning it shows me only where the word appears first (in which line). How do I include all the lines of a text? I hope my question is understandable.. – buddies Mar 20 '21 at 07:50

0 Answers0