Question:hard

Write a Python function that displays the words in which the lowercase letter 'e' appears at least twice in the text file 'Space.txt'. For example, if the file contains:

Space exploration has unlocked incredible advancements in technology and science. Since the first moon landing in 1969, space agencies have sent probes to Mars, Jupiter and beyond. The ISS, orbiting Earth at about 400 km, serves as a hub for research. With missions planned for 2030, humanity’s cosmic journey continues!

Then the function should display:

incredible advancements science. agencies serves research.

Show Hint

Split the text into words and check w.count('e') for each word. Print the word if the count is 2 or more.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Work from the expected output:
The output has six words, each with two 'e'. So the job is: pick words, count one letter inside each, and print those that pass.

Step 2: Choose a manual count:
Instead of the count() method, we can add 1 to a counter inside an inner loop. This shows clearly how the test works.

Step 3: Write the function:
def show_words():
    with open('Space.txt') as f:
        for line in f:
            for w in line.split():
                c = 0
                for ch in w:
                    if ch == 'e':
                        c += 1
                if c >= 2:
                    print(w, end=' ')

For each word, c starts at 0 and goes up by 1 for every 'e'. If c is 2 or more, the word is printed.

Step 4: Test it:
On the sample text the function printed: incredible advancements science. agencies serves research. This matches the required output. Punctuation stays attached to a word, so 'science.' and 'research.' are printed with the full stop.

Final Answer:
The function prints each word having the letter 'e' at least twice, exactly as shown in the example.
Was this answer helpful?
0