-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_student
More file actions
119 lines (96 loc) · 3.86 KB
/
class_student
File metadata and controls
119 lines (96 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'''
'''
student_data = [["Ben", {"Maths": 67, "English": 78, "Science": 72}],
["Mark", {"Maths" : 56, "Art" : 64, "History": 39, "Geography": 55}],
["Paul", {"English": 66, "History": 88}]
]
grades = ((0, "Fail"), (50, "D"), (60, "C"), (70, "B"), (80, "A"), (101, "CHEAT!"))
class Student():
def __init__(self, name, marks):
self.name = name
self.marks = marks
def print_report_card(self):
print("Report card for student", self.name)
for subject, mark in self.marks.items():
for i in grades:
if mark < i[0]:
print(subject, " : ", prev_grade)
break
prev_grade = i[1]
def add_mark(self, subject, mark):
if subject in self.marks.keys():
print(student_name, "already has a mark for ", subject)
user_input = input("Overwrite Y/N ?")
if user_input == "Y" or user_input == "y":
self.marks[subject] = mark
return "Students mark updated"
else:
return "Students mark not updated"
else:
self.marks[subject] = mark
return "Students mark added"
class Students():
def __init__(self, all_students):
self.students = []
for student, mark in all_students:
self.add_student(student, mark)
def add_student(self, student_name, marks = {}):
if self.exists(student_name):
return "Student already in database"
else:
self.students.append(Student(student_name, marks))
return "Students added"
def print_report_cards(self, student_name = None):
for student in self.students:
if student_name == None or student_name == student.name:
student.print_report_card()
def exists(self, student_name):
for student in self.students:
if student_name == student.name:
return True
return False
def add_mark(self, student_name, subject, mark):
for student in self.students:
if student.name == student_name:
return student.add_mark(subject, mark)
return "Student not found"
students = Students(student_data)
while True:
print("Welcome to the student database")
print("What can I help you with?")
print("Enter 1 to view all report cards")
print("Enter 2 to view the report card for a student")
print("Enter 3 to add a student")
print("Enter 4 to add a mark to a student")
print("Enter 5 to exit")
try:
user_choice = int(input("Choice: "))
except ValueError:
print("That's not a number I recognize")
user_choice = 0
if user_choice == 1:
students.print_report_cards()
elif user_choice == 2:
enter_student = input("Which Student?")
students.print_report_cards(enter_student)
elif user_choice == 3:
enter_student = input("Student Name?")
print(students.add_student(enter_student))
elif user_choice == 4:
enter_student = input("Student Name?")
enter_subject = input("Subject?")
num_error = True
while num_error:
num_error = False
try:
enter_mark = int(input("Mark?"))
except ValueError:
print("I don't recognize that as a number")
num_error = True
print(students.add_mark(enter_student, enter_subject, enter_mark))
elif user_choice == 5:
break
else:
print("Unknown choice")
input("Press enter to continue")
print("Thanks for using the Student Database")