Printing lines in a text file using a while loop statement or a def function

1226 views python
-2

I am having trouble with a while loop statement for the question below. This is for a txt.file.

'Write a program that allows the user to navigate through the lines of text in any text file. The program prompts the user for a filename and copies the lines of text from the file into a list. The program then prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the text in that line number.'

Please see my code.

enterfile = input("Enter the file name: ")
file = open(enterfile, 'r')
linecount = 0
for line in file:
    linecount = linecount + 1
print("The number of lines in this txt. file is", linecount)
linenum = 0
while True:
num = int(input("Please enter a line number or press 0 to quit: "))
if num >=1 and num <= linecount:
    file = open(enterfile, 'r')
    for lines in file:
        linenum = linenum + 1
        if linenum == num:
            print(lines)
else:
    if num == 0:
        print("Thanks for using the program")
        break

When I run the program, the line number does not print after I enter a line number.

Obviously, I am not using the while loop correctly here. Can someone please tell me what I am doing wrong here? Can I possibly use a def function here?

Thanks!

answered question

Did you debug your program?

You can read the file into a list using list_of_lines = list(file). Then linecount = len(list_of_lines) and you can get a requested line by using list_of_lines[num-1] (remember lists start at 0)

@JeroenHeier No, I did not. Not sure how to do that. I am just a beginner.

You are not resetting linenum between runs. You should have linenum = 0 just before the for loop if you want to keep doing it that way

Have a look here. Learning debugging the code you have made is very important.

@JohnLaRooy Thank you for the tip. The requested line is now printing. However, it is only working for every other number that is entered.

2 Answers

0

The linenum variable must be reset to 0 (linenum = 0) before the program re-enters the loop. Otherwise the linenum variable will always keep being incremented and have a value that is greater than num and will never trigger the if statement to print the line at that number.

enterfile = input("Enter the file name: ")
file = open(enterfile, 'r')
linecount = 0

for line in file:
    linecount = linecount + 1

print("The number of lines in this txt. file is", linecount)

linenum = 0

while True:
    num = int(input("Please enter a line number or press 0 to quit: "))
    if num >=1 and num <= linecount:
        file = open(enterfile, 'r')
        for lines in file:
            linenum = linenum + 1
            if linenum == num:
                print(lines)
        linenum = 0
    else:
        if num == 0:
            print("Thanks for using the program")
            break

Missing line: linenum = 0

posted this
10

Put linenum = 0 inside the while loop and your problem solved.

posted this

Have an answer?

JD

Please login first before posting an answer.