Python 正在调试,程序将不会显示用户输入 1 之外的文本行
Python Debugging, Program will not display text lines outside user input of 1
该程序的基础是向用户询问 .txt 文件并计算文件中的行数。然后程序显示文件中的行数,用户将输入一个数字以显示文件中的特定行。如果用户点击 0,程序将结束。
程序运行正常,直到我输入除 1 以外的数字或 .txt 文件中的最后一行号。程序继续显示 "Enter a line number, want to quit? Hit 0" 一遍又一遍。
inName = input("Enter the a valid file name: ")
inputFile = open(inName, "r")
count = 0
for line in inputFile:
count = count + 1
print("The file has " + str(count) + " lines.")
inputFile.close()
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
lineno = 0
break
except ValueError:
print("Try again. Line number must be between 1 and " + str(count))
while n != 0:
if n >= 0 and n <= count:
inputFile = open(inName, "r")
for line in inputFile:
lineno = lineno + 1
if lineno == n:
print(line)
inputFile.close()
else:
print("Try again. Line number must be between 1 and " + str(count))
while True:
try:
n = int(input("Enter a line number, hit 0 to quit: "))
lineno = 0
break
except ValueError:
print("Try again. Line number must be between 1 and " + str(count))
重构了您的代码并在循环中进行了一些更改,删除了您在循环中关闭文件的部分并将其替换为 break。
如果有效,试试这个:
inName = input("Enter the a valid file name: ")
inputFile = open(inName,"r")
count = 0
for line in inputFile:
count = count + 1
print("The file has "+str(count)+" lines.");
inputFile.close()
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
lineno = 0
except ValueError:
print("Try again. Line number must be between 1 and "+str(count))
if n != 0:
if n >= 0 and n <= count:
inputFile = open(inName, "r")
for line in inputFile:
if lineno == n:
print(line)
#inputFile.close()
break
else:
lineno = lineno + 1
else:
print("Try again. Line number must be between 1 and "+str(count))
else:
break
我不会解决您的代码的众多问题,因为评论和答案已经在那里完成了相当彻底的工作。相反,我想讨论您通过反复打开和关闭文件而造成的 I/O 问题。这样做很昂贵。对于一个几乎所有时间都在等待用户输入的程序来说,这可能不会引起注意,但在不需要的情况下打开和关闭文件是一个坏习惯。
我会建议两种解决方案之一来解决这个问题。如果您正在处理小文本文件,只需将整个文件加载到内存中,例如file.readlines()
:
inName = input("Enter the a valid file name: ")
with open(inName, "r") as file:
data = file.readlines()
count = len(data)
print(f"The file has {count} lines.")
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
except ValueError:
print(f"Try again. Line number must be between 1 and {count}")
else:
if n == 0:
break
print(data[n - 1])
对于大文件,我同意您一次只加载一行的技术,但您必须对此保持聪明。我会打开文件一次,创建一个 table 行开始的偏移量,然后使用 table:
在文件中移动
inName = input("Enter the a valid file name: ")
with open(inName, "r") as file:
table = [0]
table.extend(file.tell() for _ in file)
count = len(table) - 1 # last entry is size of file
print(f"The file has {count} lines.")
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
except ValueError:
print(f"Try again. Line number must be between 1 and {count}")
else:
if n == 0:
break
file.seek(table[n - 1])
print(file.readline()
该程序的基础是向用户询问 .txt 文件并计算文件中的行数。然后程序显示文件中的行数,用户将输入一个数字以显示文件中的特定行。如果用户点击 0,程序将结束。
程序运行正常,直到我输入除 1 以外的数字或 .txt 文件中的最后一行号。程序继续显示 "Enter a line number, want to quit? Hit 0" 一遍又一遍。
inName = input("Enter the a valid file name: ")
inputFile = open(inName, "r")
count = 0
for line in inputFile:
count = count + 1
print("The file has " + str(count) + " lines.")
inputFile.close()
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
lineno = 0
break
except ValueError:
print("Try again. Line number must be between 1 and " + str(count))
while n != 0:
if n >= 0 and n <= count:
inputFile = open(inName, "r")
for line in inputFile:
lineno = lineno + 1
if lineno == n:
print(line)
inputFile.close()
else:
print("Try again. Line number must be between 1 and " + str(count))
while True:
try:
n = int(input("Enter a line number, hit 0 to quit: "))
lineno = 0
break
except ValueError:
print("Try again. Line number must be between 1 and " + str(count))
重构了您的代码并在循环中进行了一些更改,删除了您在循环中关闭文件的部分并将其替换为 break。
如果有效,试试这个:
inName = input("Enter the a valid file name: ")
inputFile = open(inName,"r")
count = 0
for line in inputFile:
count = count + 1
print("The file has "+str(count)+" lines.");
inputFile.close()
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
lineno = 0
except ValueError:
print("Try again. Line number must be between 1 and "+str(count))
if n != 0:
if n >= 0 and n <= count:
inputFile = open(inName, "r")
for line in inputFile:
if lineno == n:
print(line)
#inputFile.close()
break
else:
lineno = lineno + 1
else:
print("Try again. Line number must be between 1 and "+str(count))
else:
break
我不会解决您的代码的众多问题,因为评论和答案已经在那里完成了相当彻底的工作。相反,我想讨论您通过反复打开和关闭文件而造成的 I/O 问题。这样做很昂贵。对于一个几乎所有时间都在等待用户输入的程序来说,这可能不会引起注意,但在不需要的情况下打开和关闭文件是一个坏习惯。
我会建议两种解决方案之一来解决这个问题。如果您正在处理小文本文件,只需将整个文件加载到内存中,例如file.readlines()
:
inName = input("Enter the a valid file name: ")
with open(inName, "r") as file:
data = file.readlines()
count = len(data)
print(f"The file has {count} lines.")
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
except ValueError:
print(f"Try again. Line number must be between 1 and {count}")
else:
if n == 0:
break
print(data[n - 1])
对于大文件,我同意您一次只加载一行的技术,但您必须对此保持聪明。我会打开文件一次,创建一个 table 行开始的偏移量,然后使用 table:
在文件中移动inName = input("Enter the a valid file name: ")
with open(inName, "r") as file:
table = [0]
table.extend(file.tell() for _ in file)
count = len(table) - 1 # last entry is size of file
print(f"The file has {count} lines.")
while True:
try:
n = int(input("Enter a line number, want to quit? Hit 0: "))
except ValueError:
print(f"Try again. Line number must be between 1 and {count}")
else:
if n == 0:
break
file.seek(table[n - 1])
print(file.readline()