Python,列宽问题

时间:2017-09-30 22:38:01

标签: python

我为学生创建了跟踪测试的代码,代码可以工作,但格式化已关闭。而不是偶数列,它们显示不正确。我的代码是:

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

输出如下: 阅读六个测试和分数

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?

1 个答案:

答案 0 :(得分:0)

根据该行上名称的长度改变您打印的空格数,例如print(name, ' '*(20-len(name)), score),并相应地标题标题行。