简体   繁体   English

在运行循环时读取文件?

[英]Reading a file while running a loop?

I am having difficulty running this program without creating a logic error. 我在运行该程序时遇到了困难,而又不会产生逻辑错误。 I was wondering if someone could explain to me whats wrong. 我想知道是否有人可以向我解释怎么了。 My code for the file WORKS: 我的文件代码为:

def main():
    myfile = open('tests.txt','w')
    print('Enter six tests and scores or Enter to exit')
    print('--------------------------') #I added this feature to make the code
    #more structured
    testName = input('Enter test name: ')
    while testName != '':
        score = int(input('Enter % score of this test: ')) 
        myfile.write(str(score) + '\n')
        testName = input('Enter test name: ')  
        myfile.write(testName + '\n')
    myfile.close()
    print('File was created successfully')
main()

But this code that I run to read and output the file is giving me a logic error. 但是我运行以读取和输出文件的这段代码给了我一个逻辑错误。 I know that the code is promptly written, but I don't know what is going. 我知道代码是及时编写的,但是我不知道会发生什么。 Can you please check my code and advise me why its not working.: this is the code 能否请您检查我的代码并告诉我为什么它不起作用。:这是代码

def main():
    myfile = open('tests.txt','r')
    print('Reading six tests and scores')
    print('Test\t               Score')
    print('----------------------------')
    test_score = 0
    counter = 0 #for number of tests
    line = myfile.readline()
    while line != '':
         name = line.rstrip('\n')
         score = int(myfile.readline())
         test_score += score
         print(name, score)
         line = myfile.readline()
         counter += 1
    myfile.close()
    average = test_score/ counter
    print('Average is',format(average,'.1f'))
main()

Input/output for the first program should be 第一个程序的输入/输出应为
Entering six tests and scores Enter test name objects Enter % score on this test 88 Enter test name loops Enter % score on this test 95 Enter test name selections Enter % score on this test 86 Enter test name variables Enter % score on this test 82 Enter test name files Enter % score on this test 100 Enter test name functions Enter % score on this test 80 File was created successfully 输入六个测试和分数输入测试名称对象输入此测试的百分比分数88输入测试名称的循环输入此测试的分数百分比95输入测试名称的选择项输入此测试的分数百分比86输入测试名称的变量输入此测试的分数百分比82输入测试名称文件在此测试中输入%分数100输入测试名称功能在此测试中输入%分数80文件已成功创建

output for the second program that reads the file should be: 读取文件的第二个程序的输出应为:

Reading six tests and scores TEST SCORE objects 88 loops 95 selections 86 variables 82 files 100 functions 80 Average is 88.5 读取六个测试并得分TEST SCORE对象88个循环95个选择86个变量82个文件100个功能80平均为88.5

You have two problems. 你有两个问题。 The first one is before your while loop in the write function. 第一个是在write函数的while循环之前。 You are taking the test name as input, but not writing it to the file. 您将测试名称作为输入,但未将其写入文件。

Writing the test name to the text file before the while loop solves your first problem but then leaves you with another. 在while循环之前将测试名称写入文本文件可以解决您的第一个问题,但随后会遇到另一个问题。 The way you are adding the new lines makes you end up with empty lines at the end of the file that you are then trying to read. 添加新行的方式使您最终在尝试读取的文件末尾有空行。 Move the new line to the front of what is being written. 将新行移动到正在编写的内容的前面。

    testName = input('Enter test name: ')
    myfile.write(testName)
    while testName != '':
        score = int(input('Enter % score of this test: '))
        myfile.write('\n' + str(score))
        testName = input('Enter test name: ')
        myfile.write('\n' + testName)
    myfile.close()
    print('File was created successfully')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM