您好,登錄后才能下訂單哦!
本篇文章為大家展示了利用Python編寫學生信息管理系統的方法,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
管理員類
class Administration(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self) -> list: try: Adm = open('Administration.csv', 'r') readers = csv.DictReader(Adm) Adm_dict = [dict(i) for i in readers] Adm.close() return Adm_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def see_student(self): print('當前所有的學生信息為:') for i in stu.data: print(i) print() def see_student_course(self): print('當前所有學生的選課信息為:') for i in stu.data: print(i['姓名'], '的選課信息為:', i['課程']) print() def set_course(self): name = input('請輸入你想添加的課程名') c = [i['名稱'] for i in cou.data] if name in c: print('你準備添加的課程 已經存在') else: cou.data.append({'名稱': name, '人數': 0}) print('添加成功!') print() def set_student(self): name = input('請輸入你想添加的學生的姓名') ID = input('請輸入此學生的ID') if ID in [i['ID'] for i in stu.data]: print('這個ID已經存在,添加失敗') return print('學生賬號默認密碼123456') password = 123456 stu.data.append({'姓名': name, 'ID': ID, '密碼': password, '課程': ''}) print('添加成功!') print() def set_teacher(self): name = input('請輸入你想添加的老師的姓名') ID = input('請輸入此老師的ID') if ID in [i['ID'] for i in tea.data]: print('這個ID已經存在,添加失敗') return print('老師賬號默認密碼123456') password = 123456 tea.data.append({'姓名': name, 'ID': ID, '密碼': password, '課程': ''}) print('添加成功!') print() def app_teacher_to_class(self): t_name = input('請輸入你想操作的老師名') if t_name not in [i['姓名'] for i in tea.data]: print('你想操作的老師不存在,請重新操作') return c_name = input('請輸入你想讓該老師任課的班級') if c_name not in [i['班名'] for i in cla.data]: print('你想讓老師任教的班級不存在,請重新操作') return for i in tea.data: if i['姓名'] == t_name: i['班級'] = c_name for i in cla.data: if i['班名'] == c_name: i['任課老師'] = t_name print('操作成功') def set_class(self): name = input('請輸入你想創建的班級的名稱:') if name in [i['班名'] for i in cla.data]: print('你想創建的班級已經存在,請重新操作') return if int(input('是否添加任課老師,是輸入1,否輸入0')): t_name = input('請輸入你想添加的老師名') if t_name not in [i['姓名'] for i in tea.data]: print('你想操作的老師不存在,請重新操作') return else: t_name = '' s_name_list = [] while int(input('是否添加學生,是輸入1,否輸入0')): s_name = input('輸入你想添加的學生名') if s_name not in [i['姓名'] for i in stu.data]: print('你想操作的學生不存在,請重新操作') return else: s_name_list.append(s_name) s_name = '' if s_name_list == []: cla.data.append({'班名': name, '任課老師': t_name, '學生': s_name}) print('操作成功') else: cla.data.append({'班名': name, '任課老師': t_name, '學生': s_name.join(s_name_list)}) print('操作成功') def app_student_to_class(self): s_name = input('請輸入你想操作的學生名') if s_name not in [i['姓名'] for i in stu.data]: print('你想操作的學生不存在,請重新操作') return c_name = input('請輸入你想讓該學生進入的班級') if c_name not in [i['班名'] for i in cla.data]: print('你想讓學生進入的班級不存在,請重新操作') return for i in stu.data: if i['姓名'] == s_name: i['班級'] = c_name for i in cla.data: if i['班名'] == c_name: i['任課老師'] = s_name print('操作成功') def save(self): try: j = open('Administration.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) j.close() except Exception: print('文件讀取失敗')
學生類
class Student(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self): try: Stu = open('Student.csv', 'r') readers = csv.DictReader(Stu) Stu_dict = [dict(i) for i in readers] Stu.close() return Stu_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def choice_course(self): # print(self.data) c = [i['名稱'] for i in cou.data] c_name = input('請輸入你想選擇的課程名:') if c_name not in c: print('你選擇的課程不在課程列表中') return else: self.login_data['課程']: str = self.login_data['課程'] + c_name + ' ' for i in cou.data: if i['名稱'] == c_name: i['人數'] = int(i['人數']) + 1 print('操作成功') def see_own_course(self): if self.login_data['課程'] == '': print('你未選擇任何課程') else: print(self.login_data['課程']) def save(self): try: j = open('Student.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗')
課程類
class Course(object): def __init__(self): self.data = self.__load() def __load(self): try: Cou = open('Course.csv', 'r') readers = csv.DictReader(Cou) Cou_dict = [dict(i) for i in readers] Cou.close() return Cou_dict except Exception: print('文件讀取失敗') def save(self): try: j = open('Course.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗')
教室類
class Classroom(object): def __init__(self): self.data = self.__load() def __load(self): try: Cla = open('Classroom.csv', 'r') readers = csv.DictReader(Cla) Cla_dict = [dict(i) for i in readers] Cla.close() return Cla_dict except Exception: print('文件讀取失敗') def save(self): try: j = open('Classroom.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗')
教師類
class Teacher(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self): try: Tea = open('Teacher.csv', 'r') readers = csv.DictReader(Tea) Tea_dict = [dict(i) for i in readers] Tea.close() return Tea_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def see_class(self): print('所教班級為:', self.login_data['班級']) print() def see_class_student(self): print('所教班級中的學生為:') for i in cla.data: if i['班名'] == self.login_data['班級']: print(i['學生']) print() def save(self): try: j = open('Teacher.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗')
完整代碼如下:
import csv import time class Administration(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self) -> list: try: Adm = open('Administration.csv', 'r') readers = csv.DictReader(Adm) Adm_dict = [dict(i) for i in readers] Adm.close() return Adm_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def see_student(self): print('當前所有的學生信息為:') for i in stu.data: print(i) print() def see_student_course(self): print('當前所有學生的選課信息為:') for i in stu.data: print(i['姓名'], '的選課信息為:', i['課程']) print() def set_course(self): name = input('請輸入你想添加的課程名') c = [i['名稱'] for i in cou.data] if name in c: print('你準備添加的課程 已經存在') else: cou.data.append({'名稱': name, '人數': 0}) print('添加成功!') print() def set_student(self): name = input('請輸入你想添加的學生的姓名') ID = input('請輸入此學生的ID') if ID in [i['ID'] for i in stu.data]: print('這個ID已經存在,添加失敗') return print('學生賬號默認密碼123456') password = 123456 stu.data.append({'姓名': name, 'ID': ID, '密碼': password, '課程': ''}) print('添加成功!') print() def set_teacher(self): name = input('請輸入你想添加的老師的姓名') ID = input('請輸入此老師的ID') if ID in [i['ID'] for i in tea.data]: print('這個ID已經存在,添加失敗') return print('老師賬號默認密碼123456') password = 123456 tea.data.append({'姓名': name, 'ID': ID, '密碼': password, '課程': ''}) print('添加成功!') print() def app_teacher_to_class(self): t_name = input('請輸入你想操作的老師名') if t_name not in [i['姓名'] for i in tea.data]: print('你想操作的老師不存在,請重新操作') return c_name = input('請輸入你想讓該老師任課的班級') if c_name not in [i['班名'] for i in cla.data]: print('你想讓老師任教的班級不存在,請重新操作') return for i in tea.data: if i['姓名'] == t_name: i['班級'] = c_name for i in cla.data: if i['班名'] == c_name: i['任課老師'] = t_name print('操作成功') def set_class(self): name = input('請輸入你想創建的班級的名稱:') if name in [i['班名'] for i in cla.data]: print('你想創建的班級已經存在,請重新操作') return if int(input('是否添加任課老師,是輸入1,否輸入0')): t_name = input('請輸入你想添加的老師名') if t_name not in [i['姓名'] for i in tea.data]: print('你想操作的老師不存在,請重新操作') return else: t_name = '' s_name_list = [] while int(input('是否添加學生,是輸入1,否輸入0')): s_name = input('輸入你想添加的學生名') if s_name not in [i['姓名'] for i in stu.data]: print('你想操作的學生不存在,請重新操作') return else: s_name_list.append(s_name) s_name = '' if s_name_list == []: cla.data.append({'班名': name, '任課老師': t_name, '學生': s_name}) print('操作成功') else: cla.data.append({'班名': name, '任課老師': t_name, '學生': s_name.join(s_name_list)}) print('操作成功') def app_student_to_class(self): s_name = input('請輸入你想操作的學生名') if s_name not in [i['姓名'] for i in stu.data]: print('你想操作的學生不存在,請重新操作') return c_name = input('請輸入你想讓該學生進入的班級') if c_name not in [i['班名'] for i in cla.data]: print('你想讓學生進入的班級不存在,請重新操作') return for i in stu.data: if i['姓名'] == s_name: i['班級'] = c_name for i in cla.data: if i['班名'] == c_name: i['任課老師'] = s_name print('操作成功') def save(self): try: j = open('Administration.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) j.close() except Exception: print('文件讀取失敗') class Student(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self): try: Stu = open('Student.csv', 'r') readers = csv.DictReader(Stu) Stu_dict = [dict(i) for i in readers] Stu.close() return Stu_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def choice_course(self): # print(self.data) c = [i['名稱'] for i in cou.data] c_name = input('請輸入你想選擇的課程名:') if c_name not in c: print('你選擇的課程不在課程列表中') return else: self.login_data['課程']: str = self.login_data['課程'] + c_name + ' ' for i in cou.data: if i['名稱'] == c_name: i['人數'] = int(i['人數']) + 1 print('操作成功') def see_own_course(self): if self.login_data['課程'] == '': print('你未選擇任何課程') else: print(self.login_data['課程']) def save(self): try: j = open('Student.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗') class Course(object): def __init__(self): self.data = self.__load() def __load(self): try: Cou = open('Course.csv', 'r') readers = csv.DictReader(Cou) Cou_dict = [dict(i) for i in readers] Cou.close() return Cou_dict except Exception: print('文件讀取失敗') def save(self): try: j = open('Course.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗') class Classroom(object): def __init__(self): self.data = self.__load() def __load(self): try: Cla = open('Classroom.csv', 'r') readers = csv.DictReader(Cla) Cla_dict = [dict(i) for i in readers] Cla.close() return Cla_dict except Exception: print('文件讀取失敗') def save(self): try: j = open('Classroom.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗') class Teacher(object): def __init__(self): self.data = self.__load() self.login_data = {} def __load(self): try: Tea = open('Teacher.csv', 'r') readers = csv.DictReader(Tea) Tea_dict = [dict(i) for i in readers] Tea.close() return Tea_dict except Exception: print('文件讀取失敗') def see_course(self): print('當前所有的課程信息為:') for i in cou.data: print(i) print() def see_class(self): print('所教班級為:', self.login_data['班級']) print() def see_class_student(self): print('所教班級中的學生為:') for i in cla.data: if i['班名'] == self.login_data['班級']: print(i['學生']) print() def save(self): try: j = open('Teacher.csv', 'w') writer = csv.writer(j) writer.writerow(list(self.data[0].keys())) data = [list(i.values()) for i in self.data] writer.writerows(data) except Exception: print('文件讀取失敗') def login(ID, password) -> int: # 判斷是否為管理員 for i in adm.data: if ID == i['ID'] and password == i['密碼']: adm.login_data = i return 1 # 判斷是否為學生 for i in stu.data: if ID == i['ID'] and password == i['密碼']: stu.login_data = i return 2 # 判斷是否為老師 for i in tea.data: if ID == i['ID'] and password == i['密碼']: tea.login_data = i return 3 return 0 if __name__ == '__main__': print('歡迎來到久的學生選課系統') time.sleep(0.5) print('正在加載中~') adm = Administration() stu = Student() cou = Course() tea = Teacher() cla = Classroom() time.sleep(0.3) print('加載完畢') while True: ID = input('請輸入你的賬號:') password = input('請輸入你的密碼:') flag = login(ID, password) if not flag: print('輸入的賬號或密碼錯誤') flag_1 = int(input('是否重新登錄 重新登錄輸入1 取消登錄輸入0')) if flag_1: continue else: break elif flag == 1: print('歡迎管理員') break elif flag == 2: print('歡迎學生') break elif flag == 3: print('歡迎老師') break if flag == 1: while True: check = int( input( '1.創建課程\n2.創建學生賬號\n3.查看所有課程\n4.查看所有學生\n5.查看所有學生的選課信息\n6.創建講師' '\n7.為講師指定班級\n8.創建班級\n9.為學生指定班級\n10.退出程序')) if check == 1: adm.set_course() elif check == 2: adm.set_student() elif check == 3: adm.see_course() elif check == 4: adm.see_student() elif check == 5: adm.see_student_course() elif check == 6: adm.set_teacher() elif check == 7: adm.app_teacher_to_class() elif check == 8: adm.set_class() elif check == 9: adm.app_student_to_class() elif check == 10: break elif flag == 2: while True: check = int(input('1.查看所有課程\n2.查看所選課程\n3.選擇課程\n4.退出程序')) if check == 1: stu.see_course() elif check == 2: stu.see_own_course() elif check == 3: stu.choice_course() elif check == 4: break elif flag == 3: while True: check = int(input('1.查看所有課程\n2.查看所教班級\n3.查看班級中的學生\n4.退出程序')) if check == 1: tea.see_course() elif check == 2: tea.see_class() elif check == 3: tea.see_class_student() elif check == 4: break print('數據存儲中~') adm.save() tea.save() stu.save() cla.save() cou.save() time.sleep(0.5) print('感謝您的使用!')
上述內容就是利用Python編寫學生信息管理系統的方法,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。