Skip to content

Instantly share code, notes, and snippets.

@RavinduSachintha
Created July 24, 2019 14:34
Show Gist options
  • Save RavinduSachintha/3cc2d2d41e32b382ff59ce4b2da52424 to your computer and use it in GitHub Desktop.
Save RavinduSachintha/3cc2d2d41e32b382ff59ce4b2da52424 to your computer and use it in GitHub Desktop.
Calucate the GPA according to the given grade values. Both files should be in the same folder. In results file, use the same structure to add more records. (structure :- <SUBJECT_CODE> <GRADE> <CREDIT> )
f = open("results.txt","r");
globalGPA = 0
localGPA = 0
localCreditValues = 0
localCreditNumber = 0
totalCreditNumber = 0
numberOfSems = 0
def convertGradeToValue(grade):
switcher = {
"A+": 4.25,
"A": 4.00,
"A-": 3.75,
"B+": 3.25,
"B": 3.00,
"B-": 2.75,
"C+": 2.25,
"C": 2.00,
"C-": 1.75,
"D+": 1.25,
"D": 1.00,
"D-": 0.75,
"E": 0.00
}
return switcher.get(grade, "nothing")
for x in f:
lineBody = x.split(" ")
if(lineBody[0]=="##"):
if(localCreditValues != 0 and localCreditNumber !=0 and lineBody[1] != "Final"):
totalCreditNumber = totalCreditNumber + localCreditNumber
currentGPA = localCreditValues / localCreditNumber
print("\nTotal Number of Credits for the year : " + str(localCreditNumber))
print("Current GPA for the year : " + str(round(currentGPA,5)))
globalGPA = globalGPA + currentGPA
print("\n" + x)
if(lineBody[1] == "Final"):
print("Total Credits for all the years : " + str(totalCreditNumber))
print("Final GPA for all the years : " + str(round(globalGPA / numberOfSems,5)))
elif(lineBody[1] != "results"):
print("Results for courses : ")
localCreditValues = 0
localCreditNumber = 0
numberOfSems = numberOfSems + 1
elif(lineBody[0]=="-"):
print(lineBody[1] + " => " + lineBody[2])
localCreditValues = localCreditValues + convertGradeToValue(lineBody[2]) * int(lineBody[3])
localCreditNumber = localCreditNumber + int(lineBody[3])
f.close()
## 1st year - 1st sem
- DSA_1 B 3
- PRO_1 A 3
- DB_1 C 3
- MM_1 A 2
- CSY A- 2
- LAB_1 A 2
## 1st year - 2nd sem
- SE_1 A 2
- DSA_2 B 2
- PRO_2 B- 3
- DIS B 2
- MM_2 B 2
- FCS B 2
- STA A- 2
## results included completely
## Final Results :::::::
@Danushka96
Copy link

Shall we make this Application available on web? 😄

@RavinduSachintha
Copy link
Author

Shall we make this Application available on web? smile

Need to store results. otherwise useless. if you will provide a server, 🙊 let's do it. (after the exam 😅)

@Danushka96
Copy link

No problem! 🦊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment