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

Python调试时,程序将不会显示用户输入1以外的文本行,python,python-3.7,Python,Python 3.7,该程序的基础是向用户索要一个.txt文件,并计算文件中的行数。然后程序显示文件中的行数,用户将输入一个数字以显示文件中的某一行。如果用户点击0,程序将结束 程序运行正常,直到我在.txt文件中输入除1之外的数字或最后一行号。程序继续反复显示“输入行号,想退出?点击0” inName = input("Enter the a valid file name: ") inputFile = open(inName, "r") count = 0 for line in inputFile:

该程序的基础是向用户索要一个.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))
inName=input(“输入有效文件名:”)
inputFile=open(inName,“r”)
计数=0
对于inputFile中的行:
计数=计数+1
打印(“文件有“+str(count)+”行。”)
inputFile.close()
尽管如此:
尝试:
n=int(输入(“输入行号,想退出吗?点击0:”)
lineno=0
打破
除值错误外:
打印(“重试。行号必须介于1和“+str(计数))
而n!=0:

如果n>=0且n重构了代码并在循环中做了一些更改,请删除关闭文件中间循环的部分,并将其替换为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
inName=input(“输入有效文件名:”)
inputFile=open(inName,“r”)
计数=0
对于inputFile中的行:
计数=计数+1
打印(“文件有“+str(count)+”行。”);
inputFile.close()
尽管如此:
尝试:
n=int(输入(“输入行号,想退出吗?点击0:”)
lineno=0
除值错误外:
打印(“重试。行号必须介于1和“+str(计数))
如果n!=0:

如果n>=0且n我将不讨论代码中的大量问题,因为注释和答案已经完成了相当彻底的工作。相反,我想通过反复打开和关闭文件来讨论您正在创建的I/O问题。这样做很昂贵。对于一个几乎所有时间都在等待用户输入的程序来说,可能不会引起注意,但不需要打开和关闭文件是一个坏习惯

我会建议两种解决方案中的一种来解决这个问题。如果您处理的是小文本文件,只需将整个文件加载到内存中,例如使用
file.readlines()

对于大文件,我同意您一次只加载一行的技术,但您必须对此保持明智。我会打开文件一次,创建一个线起点偏移表,然后使用该表在文件中移动:

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

这个代码是意大利面,你在关闭中间的文件,而不打破,为什么“代码> LeNeNO<代码>第一个初始化在一个完全不相关的范围内?考虑使用<代码>打开()/<代码>,而不是手动管理FiffiDAK以供您的帮助!@j、 海滩。很高兴你成功了。向上投票将有助于感谢你的帮助!
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()