0

I created code for tracking tests for my students, the code works but the formatting is off. Instead of even columns they are displayed incorrectly. The code I have is:

    def main():
        mytests = open('test.txt','r')
        count = 0
        total = 0

        print('Reading six tests and scores')
        print()
        print('TEST      SCORE')

    for line in mytests:
        name = line.rstrip('\n')
        score = int(mytests.readline())
        print(name,'        ', score)
        count += 1
        total += score 
    mytests.close()
    print('Average: ',format(total/count,'.1f'))

main()

The output looks like: Reading six tests and scores

TEST      SCORE
History          98
Math          89
Geology          78
Space          78
PE          90
Religion          75
Average:  84.7

Does anybody have any ideas on how I can format the score column to be in alignment?
  • The existing question above may solve your problem but if I had this issue I would write the data to .csv which is tab separated by default. If you are on a UNIX based os you can simply `cat` the file and it will look well I believe. – Dean Coakley Sep 30 '17 at 22:45

1 Answers1

0

Vary the number of spaces you print based on the length of the name on that line, e.g. print(name, ' '*(20-len(name)), score), and space the header line accordingly.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80