Skip to content

Instantly share code, notes, and snippets.

@gbozee
Created October 18, 2017 17:55
Show Gist options
  • Save gbozee/7906af039c6b4c8fe4877605b4f80d33 to your computer and use it in GitHub Desktop.
Save gbozee/7906af039c6b4c8fe4877605b4f80d33 to your computer and use it in GitHub Desktop.
import csv
def get_input(passed):
"""This function converts a string to an integer
and returns None when the string isn't an integer"""
try:
number_of_courses = int(passed)
except ValueError:
number_of_courses = None
return number_of_courses
def get_grade(_input):
options = ['a', 'b', 'c', 'd', 'e', 'f']
options.extend([x.upper() for x in options])
if _input in options:
return _input
return None
def single_func():
return get_input(23)
def populate_data():
course = input("Enter the name of the course: ")
unit = repeat_when_wrong(get_input, "Enter the unit of {}: ".format(course),
"Please input an integer")
grade = repeat_when_wrong(get_grade, "Enter the grade of {}: ".format(course),
"Please ensure you select either A, B, C, D, E, F")
return course, unit, grade
# while not unit:
def repeat_when_wrong(func, text, error_message):
result = func(input(text))
while not result:
print(error_message)
result = func(input(text))
return result
def grade_score(grade):
"""Returns the actual score based on a grade."""
options = {
'a': 5,
'b': 4,
'c': 3,
'd': 2,
'e': 1,
'f': 0
}
return options[grade.lower()]
def read_csv(csv_file_name):
with open(csv_file_name) as csv_file:
content = csv.reader(csv_file)
import pdb; pdb.set_trace()
class GPACalculator(object):
def __init__(self, display_year=None, print_statement=True):
self.courses = []
if display_year:
print("This is the data for Year {}".format(display_year))
self.get_user_input()
self.calculate_gpa()
if print_statement:
print("Your GPA score is {}".format(self.gpa))
def calculate_gpa(self):
self.total_weight_score = 0
self.total_number_of_units = 0
for course in self.courses:
ws = course['unit'] * course['grade_score']
self.total_weight_score += ws
self.total_number_of_units += course['unit']
self.gpa = self.total_weight_score / self.total_number_of_units
# return
def get_user_input(self):
self.number_of_courses = repeat_when_wrong(
get_input,
"how many courses did you take: ",
"Please we are serious here, input an actual number of courses.")
for i in range(self.number_of_courses):
course, unit, grade = populate_data()
self.courses.append({
'course': course,
'unit': unit,
'grade': grade,
'grade_score': grade_score(grade)
})
@classmethod
def calculate_cumulative_gpa(cls, number_of_years):
all_years = [cls(x+1, False) for x in range(number_of_years)]
for index, value in enumerate(all_years):
print("The GPA for the year {} is {}".format(
index + 1, value.gpa))
total_weighted_score = sum([x.total_weight_score for x in all_years])
total_units = sum([x.total_number_of_units for x in all_years])
print("Your Cummulative GPA is {}: ".format(total_weighted_score / total_units))
# instance = GPACalculator()
# GPACalculator.calculate_cumulative_gpa(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment