Reputation: 13
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!
Upvotes: 0
Views: 11669
Reputation: 1
Polished version:
enterfile = input("Enter the input file name: ")
file = open(enterfile, 'r')
linecount = 0
for line in file:
linecount = linecount + 1
print("The file has",linecount,"lines.")
while True:
linenum = 0
num = int(input("Enter a line number [0 to quit]: "))
if num >=1 and num <= linecount:
file = open(enterfile, 'r')
for lines in file:
linenum = linenum + 1
if linenum == num:
print(num,":",lines)
elif num == 0:
break
else:
if num!= linecount:
print("ERROR: line number must be less than",linecount)
Upvotes: 0
Reputation: 31
use readlines() function return a list that containing the lines then print index value according to user input.
file = input("Enter the file name: ")
text_file = open(file, "r")
lines = text_file.readlines()
print (len(lines))
while True:
linenumber = int(input("Please enter a line number or press 0 to quit: "))
if linenumber == 0:
print("Thanks for using the program")
break
elif 1 <= linenumber <= len(lines) :
print (lines[linenumber- 1])
else:
print("Please enter valid line number")
text_file.close()
Upvotes: 1
Reputation: 112
Move line linenum = 0
inside the While True:
loop.
The linenum
variable must be reset to 0 (linenum = 0
) when 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.
Your code with linenum = 0
in the loop:
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)
while True:
linenum = 0
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
Alternative method:
enterfile = input("Enter the file name: ")
with open(enterfile) as f:
lines = [line.rstrip() for line in f]
print("The number of lines in this txt. file is", len(lines))
while True:
num = int(input("Please enter a line number or press 0 to quit: "))
if num > 0 and num < len(lines) + 1:
print(lines[num - 1])
elif num == 0:
print('Thanks for using the program.')
break
Upvotes: 1
Reputation: 191894
Seems like you missed the first step of the assignment after the input
copies the lines of text from the file into a list
With that, you would have
with open(enterfile) as f:
lines = [line.rstrip() for line in f]
# continue on from here
Now, forget you even have a file, you can use len(lines)
and lines[number-1]
to get the total lines and a specific line, respectively
Upvotes: 3