python - How to calculate GPA assignment -
this question has answer here:
- gpa python assignment [closed] 1 answer
i have programming assignment opens file , calculates on gpa.
wherein a= 4, b = 3, c = 2, d = 1, e = 0 .txt file contains:
ecs10 4
ecs20 b 3
jpn4 c 5
phy9a d 5
the last element amount of credits , letters before grades. able last element , letters im stuck. keep using if statements in loop numbers out of letters cant seem make work. (im new @ programming kind of appreciated) thanks!
here's i've got far:
f = open("grade_file.txt", "r")
line = f.readlines()
isolates numbers
for in (line):
i = i.strip() print(i[-1:])
isolates letters
print("\n")
for in (line):
i = i.strip() print(i[-3:-2])
gpa calculated multiplying amount of credits grade received , divided total number of credits.
try piece of code:
#!/usr/bin/env python3.3-32 grade_mapping = {'a':4, 'b':3, 'c':2, 'd':1, 'e':0} weighted_sum = 0 credits = 0 line in open("grade_file.txt", "r"): elems = line.split(' ') weighted_sum += grade_mapping[elems[1]] * int(elems[2]) credits += float(elems[2]) gpa = weighted_sum / credits print(gpa)
i used file test:
ecs10 4 ecs20 b 3 jpn4 c 5 phy9a d 5
the result should 2.35294117647
Comments
Post a Comment