我必須編寫一個程序來導入文本文件,計算測試平均值,然後將其打印到表格中。除了表格的格式外,我能夠讓所有的東西都能工作。形成表格
下面是表應該如何看
讀6次測試,成績
TEST---------------SCORE
objects------------88
loops--------------95
selections---------86
variables----------82
files--------------100
functions----------80
Average is
我無法弄清楚如何讓物體,循環,選擇等會馬上下對方這裏。但是,桌子應該如何設置。我只是無法將分數排列在分數列中。
這是我的代碼。
def main():
print('Reading six tests and scores')
print('TEST\tSCORE')
test_scores = open('tests.txt', 'r')
total_score = 0
counter = 0
line = test_scores.readline()
while line != '':
name = line.rstrip('\n')
score = int(test_scores.readline())
total_score += score
print(name, score, sep='\t')
line = test_scores.readline()
counter += 1
test_scores.close()
average_test = total_score/counter
print('Average is', format(average_test, '.1f'))
main()
[在Python中表格格式化文本]的可能重複(http://stackoverflow.com/questions/16110230/formatting-text-in-a-table-in-python) –