简体   繁体   中英

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

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

You have two problems. The first one is before your while loop in the write function. 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. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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