Advertisements
Advertisements
Question
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.
Code Writing
Advertisements
Solution
def display_words():
file = open("Space.txt", "r")
content = file.read()
words = content.split()
for word in words:
# Strip punctuation to match example output exactly
clean_word = word.strip(".")
if clean_word.count('e') >= 2:
print(clean_word, end=" ")
file.close()shaalaa.com
Is there an error in this question or solution?
