Answer:
#section 1
filename = input('Enter file name: ')
with open(filename,'r') as file:
lines = []
for line in file:
lines.append(line.strip())
print('Total lines in the file:', len(lines))
#section 2
while True:
line_number = int(input('Enter line number: '))
if line_number == 0:
break
else:
index = line_number - 1
print(lines[index])
Explanation:
The programming language utilized is Python
# section 1
In this part, the program prompts the user for the file's name. It opens the file using a WITH statement, ensuring that it closes automatically when done.
An empty list is created to store the lines from the file.
A FOR loop is implemented to read through the file, adding each line to the list. The .strip() function removes any unwanted characters such as \t and \n from the text.
At the end of this section, the total line count is displayed using the len() function.
#section 2
<pIn this section, a WHILE loop is initiated to continue running until the user enters 0. The loop requests a line number from the user and then outputs that line's content to the screen.
I have included the result as an image.