Last active
September 23, 2018 07:46
-
-
Save manojd929/bb486bdf726712f086fdd6e058ab86b7 to your computer and use it in GitHub Desktop.
CRUD, mean, std deviation subject - student
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class ClassRoom: | |
students = { } | |
mean = 0 | |
stdDeviation = 0 | |
def get_total_students(self): | |
return len(self.students) | |
def get_all_students(self): | |
return self.students.items() | |
def get_mean_score(self): | |
total = 0 | |
for usn in self.students.keys(): | |
total += self.students[usn]['score'] | |
self.mean = total / len(self.students) | |
return self.mean | |
def get_std_deviation(self): | |
total = 0 | |
for usn in self.students: | |
total += math.pow(self.students[usn]['score'] - self.mean, 2) | |
self.stdDeviation = math.sqrt(total/len(self.students)) | |
return self.stdDeviation | |
def create_student(self, name, usn, score): | |
if name != '' or usn != '' or not (score < 0 and score > 100): | |
student = { 'name': name, 'score': score } | |
self.students[usn] = student | |
print "Created: Student record for usn: ", usn, student | |
else: | |
print "Invalid input: Record creation failed" | |
def get_student_info(self, usn): | |
if usn != '' and self.students.has_key(usn): | |
print "Get usn: ", usn, self.students[usn] | |
else: | |
return "No student record found for usn: ", usn | |
def update_student_info(self, usn, name, score): | |
if usn != '' and self.students.has_key(usn): | |
if name != '' or not (score < 0 and score > 100): | |
student = { 'name': name, 'score': score } | |
self.students[usn] = student | |
print "Updated: Student record for usn: ", usn, student | |
else: | |
return "No student record found for usn: ", usn | |
def delete_student_info(self, usn): | |
if usn != '' and self.students.has_key(usn): | |
record = self.students.get(usn) | |
del self.students[usn] | |
print "Deleted: Student record ", usn, record | |
else: | |
return "No student record found for usn: ", usn | |
def main(): | |
c = ClassRoom() | |
c.create_student("Sona1", "12341", 45) | |
c.create_student("Sona2", "12342", 44) | |
c.create_student("Sona3", "12343", 43) | |
c.create_student("Sona4", "12344", 42) | |
c.create_student("Sona5", "12345", 41) | |
print "All student info, ", c.get_all_students() | |
print "Total number of students: ", c.get_total_students() | |
print "Mean Score: ", c.get_mean_score() | |
print "Standard Deviation: ", c.get_std_deviation() | |
c.get_student_info("12341") | |
c.delete_student_info("12345") | |
print "Total number of students: ", c.get_total_students() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment