What have you done/learned
import csv
from decimal import Decimal, ROUND_DOWN
filename = 'GPA_Calculate.csv'
grade = 0
total_grade = 0
credit = 0
total_credit = 0
gpax = 0
grade_list = ['A', 'B+', 'B', 'C+', 'C', 'D+', 'D', 'F']
score_list = [4, 3.5, 3, 2.5, 2, 1.5, 1, 0]
def gpa_calculate(subject):
for i in range(len(grade_list)):
if subject[3] == grade_list[i]:
return Decimal(score_list[i] * float(subject[2]))
with open(filename) as in_file:
last_line = sum(1 for _ in in_file)
reader = csv.reader(open(filename))
for row in reader:
if row[3] == '':
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
grade = 0
credit = 0
elif last_line == reader.line_num:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
else:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])