Python调试时,程序不会显示用户输入1以外的文本行

2024-03-28 20:19:03 发布

您现在位置:Python中文网/ 问答频道 /正文

该程序的基本功能是向用户请求一个.txt文件,并计算文件中的行数。然后程序显示文件中的行数,用户将输入一个数字来显示文件中的某一行。如果用户点击0,程序将结束

程序运行正常,直到我在.txt文件中输入除1以外的数字或最后一行数字。程序继续显示“输入行号,想退出吗?一次又一次地打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))

Tags: and文件用户程序numberinputcountline
2条回答

重构代码并在循环中做了一些更改,删除了关闭文件中间循环的部分并用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])

对于大文件,我同意你一次只加载一行的技术,但是你必须聪明一点。我会打开一次文件,创建一个到行首的偏移表,然后使用该表在文件中移动:

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()

相关问题 更多 >