Skip to content

Instantly share code, notes, and snippets.

@fkztw
Last active December 29, 2015 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fkztw/7672041 to your computer and use it in GitHub Desktop.
Save fkztw/7672041 to your computer and use it in GitHub Desktop.
NCTU GPA calculator written in python2 Input Example: 成績1,學分數1 成績2,學分數2 成績3,學分數3 ...
# -*-coding: utf-8-*-
def find_gpa(grade):
if 100 >= grade >= 90:
return [grade, 'A+', 4.3]
elif 89 >= grade >= 85:
return [grade, 'A', 4.0]
elif 84 >= grade >= 80:
return [grade, 'A-', 3.7]
elif 79 >= grade >= 77:
return [grade, 'B+', 3.3]
elif 76 >= grade >= 73:
return [grade, 'B', 3.0]
elif 72 >= grade >= 70:
return [grade, 'B-', 2.7]
elif 69 >= grade >= 67:
return [grade, 'C+', 2.3]
elif 66 >= grade >= 63:
return [grade, 'C', 2.0]
elif 62 >= grade >= 60:
return [grade, 'C-', 1.7]
elif 59 >= grade >= 50:
return [grade, 'D', 1.0]
elif 49 >= grade >= 1:
return [grade, 'E', 0.0]
elif grade is 0:
return [grade, 'X', 0.0]
else:
return None
def main():
#Input Example: 成績1,學分數1 成績2,學分數2 成績3,學分數3 ...
g = raw_input('Enter your grade: ')
g = g.strip().split()
l = []
for x in g:
x = x.split(',')
t = find_gpa(int(x[0]))
if t is not None:
t.append(x[1])
l.append(t)
else:
raise BaseException('You have wrong grade.')
avg = 0.0
gpa = 0.0
weights = 0
for x in l:
print('{0:4d} {1:2s} {2:1.1f} {3}'.format(x[0], x[1], x[2], x[3]))
avg += float(x[0])*int(x[3])
gpa += float(x[2])*int(x[3])
weights += int(x[3])
print('your AVG grade: {0}'.format(avg/weights))
print('your GPA: {0}'.format(gpa/weights))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment