Skip to content

Instantly share code, notes, and snippets.

@idoqo
Last active August 3, 2019 01:35
Show Gist options
  • Save idoqo/924e07236ff2e921da17224315d43491 to your computer and use it in GitHub Desktop.
Save idoqo/924e07236ff2e921da17224315d43491 to your computer and use it in GitHub Desktop.
class Student:
def __init__(self, name, age):
fname = name.strip()+".txt"
student_file = open(fname, 'w')
student_file.write("Name: %s \n" % (name))
student_file.write("Age: %d \n" % (age))
student_file.close()
self.student_file = fname
self.name = name
self.age = age
self.total_number_of_units = 0
def collect_raw_scores(self):
raw_scores = {}
while True:
course = input("Enter course or 'quit' to stop:")
if (course == "quit"):
break
unit_weight = int(input("Enter unit weight of %s "%(course)))
score = int(input("Enter score for %s: "%(course)))
raw_scores.update({course: [unit_weight, score]})
with open(self.student_file, "a") as f:
f.write("\n")
print(raw_scores, file=f)
return raw_scores
def points_for_semester(self, semester_scores):
total_grade_points = 0
total_units = 0
for course,weight_and_score in semester_scores.items():
weight = weight_and_score[0]
score = weight_and_score[1]
total_units += weight
if (0 <= score <= 39):
point = 0 * weight
elif (40 <= score <= 44):
point = 1 * weight
elif (45 <= score <= 49):
point = 2 * weight
elif (50 <= score <= 59):
point = 3 * weight
elif (60 <= score <= 69):
point = 4 * weight
elif (70 <= score <= 100):
point = 5 * weight
total_grade_points += point
self.total_number_of_units += total_units
return [total_grade_points, total_units]
if __name__ == "__main__":
name = input("Enter Student's name: ")
age = int(input("Enter student's age"))
student = Student(name, age)
total_points = 0
total_units = 0
for i in range(0,6):
print("Collecting data for Semester '%d':\n"%(i+1))
semester_scores = student.collect_raw_scores()
points_per_sem = student.points_for_semester(semester_scores)
total_points += points_per_sem[0]
total_units += points_per_sem[1]
print("CGPA for %s is %f" % (student.name, total_points/total_units))
@Taycode
Copy link

Taycode commented Aug 3, 2019

this should go

@idoqo
Copy link
Author

idoqo commented Aug 3, 2019

this should go

๐Ÿ™‡๐Ÿ™‡

@emmacode
Copy link

emmacode commented Aug 3, 2019

yah it should

@Paulthaprodigy
Copy link

This is wonderful.
I'm starting to feel like they are giving us some of their problems in form of tutorial questions

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