English

Write a Python function that displays all the lines containing the word 'vote' from a text file "Elections.txt". For example, if the file contains: In an election many people vote to choose their

Advertisements

Question

Write a Python function that displays all the lines containing the word 'vote' from a text file "Elections.txt". For example, if the file contains:

In an election many people vote to choose their representative.
The candidate getting the maximum share of votes stands elected.
Normally, one person has to vote oncе.
The process of voting may vary with time and region.
Then the output should be:
In an election many people vote to choose their representative.
Normally, one person has to vote once.

Code Writing
Advertisements

Solution

def vote():
    f = open("Elections.txt")
    slst = f.readlines()
    for line in slst:
        wordlst = line.split()
        if "vote" in wordlst:
            print(line)
    f.close()
shaalaa.com
  Is there an error in this question or solution?
2024-2025 (March) Set 4

APPEARS IN

Advertisements
Share
Notifications



      Forgot password?
Use app×