-
Notifications
You must be signed in to change notification settings - Fork 247
/
1.student.mark.py
239 lines (209 loc) · 8.24 KB
/
1.student.mark.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
students_list = []
courses_list = []
marks_list = []
# Input a number of students in an class (done)
def input_student(students_list):
# Ask the user how many students would they like to add
while True:
try:
no_students = int(input("Input number of students to be added in the class: "))
if no_students <= 0:
print("There must be at least one student")
else:
break
except:
print("Must be an integer!")
# Inputting neccessary info for students
for s in range(0, no_students, 1):
while True:
try:
student_dict = {}
print(f"---Student {s}. ")
student_id = int(input("Student ID: "))
student_name = str(input("Name: "))
student_dob = str(input("D.O.B (DD/MM/YYYY): "))
if student_id <= 0 or student_name == "" or student_dob == "":
print("try again")
else:
# Check if the student exist yet
for student in students_list:
name = student.get("name")
_id = student.get("id")
if student_name == name or student_id == _id:
print("Student already exist!")
return
student_dict.update({"id":student_id})
student_dict.update({"name":student_name})
student_dict.update({"dob":student_dob})
students_list.append(student_dict)
break
except:
print("Something went wrong, try again.")
# List out all students in the class (Done)
def list_student(students_list):
print("---List of students in the class---")
counter = 1
for student_dict in students_list:
# Only print out the student's info if all keys exists
if "name" and "id" and "dob" in student_dict:
student_name = student_dict.get("name")
student_id = student_dict.get("id")
student_dob = student_dict.get("dob")
print(f"Student {counter}. Name: {student_name}, ID: {student_id}, D.O.B: {student_dob}")
counter = counter + 1
# Input a number of courses (Done)
def input_course(course_list):
# Ask the user how many courses they would like to add
while True:
try:
no_courses = int(input("Input number of courses to be added: "))
if no_courses <= 0:
print("There must be at least one course")
else:
break
except:
print("Must be an integer!")
# Inputting neccessary info for the courses
for c in range(0, no_courses, 1):
while True:
try:
print(f"---Course {c}. ")
course_dict = {}
course_name = str(input("Course Name: "))
course_id = int(input("Course ID: "))
if course_id <= 0 or course_name == "":
print("try again")
else:
# Check if the course exist yet
for course in course_list:
name = course.get("name")
_id = course.get("id")
if course_name == name or course_id == _id:
print("Course already exist!")
return
course_dict.update({"name":course_name})
course_dict.update({"id":course_id})
course_list.append(course_dict)
break
except:
print("Something went wrong, try again.")
# List out available courses (Done)
def list_course(courses_list):
counter = 1
print("---List of courses available---")
for course_dict in courses_list:
if "name" and "id" in course_dict:
course_name = course_dict.get("name")
course_id = course_dict.get("id")
print(f"Course {counter}. Name: {course_name}, ID: {course_id}")
counter = counter + 1
# Select a course, input marks for a student in that courses
def input_mark(students_list, courses_list, marks_list):
student_exist = False
course_exist = False
student_name = input("Student's name: ")
# Check if the student exist
if len(students_list) == 0:
print("Student does not exist!")
return
else:
for student_dict in students_list:
name = student_dict.get("name")
if student_name == name:
student_exist = True
course_name = input("Course's name: ")
# Check if the course exist
if len(courses_list) == 0:
print("Course does not exist!")
return
else:
for course_dict in courses_list:
name = course_dict.get("name")
if course_name == name:
course_exist = True
# If student or course does not exist, then the function stops
if student_exist == False:
print("Student does not exist!")
return
if course_exist == False:
print("Course does not exist!")
return
# Ask the user the student's mark for that course
student_mark = 0
while True:
try:
student_mark = float(input("Input mark here: "))
if student_mark < 0:
print("Mark must be non-negative")
else:
break
except:
print("Must be a number!")
# The student mark will be a dictionary
student_mark_dict = {}
student_mark_dict.update({"student_name":student_name})
student_mark_dict.update({"course_name":course_name})
student_mark_dict.update({"student_mark":student_mark})
# Add the mark's info into the list
marks_list.append(student_mark_dict)
# List out all mark of a student
def list_mark(marks_list):
search_target = input("Whose mark do you want to see?: ")
for mark_dict in marks_list:
if "student_name" and "course_name" and "student_mark" in mark_dict:
student_name = mark_dict.get("student_name")
course_name = mark_dict.get("course_name")
student_mark = mark_dict.get("student_mark")
# Display the mark of a specific student
if search_target == student_name:
print(f"Student: {student_name}, Course: {course_name}, Mark: {student_mark}")
# Execute the code
# This code will loop until terminated
if __name__ == "__main__":
print("------Student Mark management system------")
while True:
print("---Here are the things that you could do---")
print("1. Add new student(s)")
print("2. Add new course(s)")
print("3. Select a course and input mark for student")
print("4. List out all students")
print("5. List out all courses")
print("6. List out all students' mark for each courses")
print("7. Exit")
op = input("What do you want to do? (1-7): ")
# Use this code if you're using a Python version 3.10 or later
match op:
case "1":
input_student(students_list)
case "2":
input_course(courses_list)
case "3":
input_mark(students_list, courses_list, marks_list)
case "4":
list_student(students_list)
case "5":
list_course(courses_list)
case "6":
list_mark(marks_list)
case "7":
exit()
case _:
print("Invalid input")
# Use this code if you're using a Python version earlier than 3.10
# if op == "1":
# input_student(students_list)
# elif op == "2":
# input_course(courses_list)
# elif op == "3":
# input_mark(students_list, courses_list, marks_list)
# elif op == "4":
# list_student(students_list)
# elif op == "5":
# list_course(courses_list)
# elif op == "6":
# list_mark(marks_list)
# elif op == "7":
# exit()
# else:
# print("Invalid input")
print("\n")