基于Python的学生选课信息管理系统(列表与文件的应用)

课程 (Course) 问题求解与程序设计 (Problem Solving and Programming)
列表与文件的应用 (Application of lists and files)             评分 (Score)
一、实验目的( Lab Objectives
1 、理解列表数据类型的基本概念。( Understand the fundamental conceptions about lists.
2 、掌握列表数据类型的操作方法。( Master the operations of lists.
3 、理解文件的基本概念。( Understand the fundamental concepts of files.
4 、掌握文件操作方法。( Master the operations of files.
5 、掌握列表与文件的应用。( Master the applications of lists and files.
二、实验内容( Lab Contents
编写 Python 程序,完成学生的选课信息管理。 (Writing Python programs to manage students’ courses selection.)
课程信息存储在文件 courses.txt 中,每门课程信息包括课程名、课程编号、课程学分、主讲讲师姓名、上课地
点(假设每门课程的上课地点是唯一的)、课程的其他信息(如多少实验课时、多少理论课时等)。
(Courses’ information
will be stored in a text file called courses.txt. The courses’ information will include COURSE’S NAME, ID, CREDIT, TEACHER’S NAME, ADDRESS (assuming that a course has only one address), and other information about the course, such as lab hours and lecture hours. )
文件 courses.txt 的示例内容与格式如下所示: (An example about the content and the format of the courses.txt file is as follows.)
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week.
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.
Programming Fundamental,31060104,3.5,Donald E. KNUTH,9-203
The course includes 28 lab hours.
上面的示例文件中,包含有 4 门课程的信息。 (The example file includes 4 courses’ information.)
如第一行是课程“ Academic English I ”,其编号为“ 31060101 ”,学分为“ 3 ”,主讲教师为“ Liang ZHU-GE ”, 上课地点为“8-102 ”。第二行则是该课程的其他信息“ Two hours each week-day. ”。 (The first line in the file shows that the course’s name is “Academic English I”, its ID is “31060101”, the credit of the course is “3”, the teacher’s name is “Liang ZHU-GE”, and the classroom number is “8-102”. The second line shows the other information such as “Two hours each week-day”.)
类似的,第三行、第四行是第二门课“ Advanced Mathematics I1 ”的相关信息;第五行、第六行是第三门课 “Introduction to IT ”的相关信息;第七行、第八行是第四门课“ Programming Fundamental ”的相关信息。 (Similarly, the third line and the fourth line show the information about the second course, “Advanced Mathematics I1”; the 5th line and the 6th line show about the third course, “Introduction to IT”, and the 7th line and the 8th line show about the fourth course, “Programming Fundamental”.)
学生信息存储在文件 students.txt 中。每位学生的信息包括学生姓名、学号、性别、专业、选课数目、选课的课 程编号列表。(Students’ information will be stored in a text file called students.txt. The students’ information will include STUDENT’S NAME, ID, GENDER, NUMBER OF SELECTED COURSES, THE SELECTED COURSES’ LIST. )
文件 students.txt 的示例内容与格式如下所示:
(An example about the content and the format of the students.txt file is as follows.)
Harry POTTER,1766060101,m,Computer Science and Technology
4
31060101
31060102
31060103
31060104
WuKong SUN,1766060102,m,Computer Science and Technology
2
31060103
31060104
XiaoTiao MA,1766060103,m,Computer Science and Technology
1
31060104
MuLan HUA,1766060104,f,Computer Science and Technology
3
31060102
31060103
31060104
Bill GATES,1766060105,m,Computer Science and Technology
0
上面的示例文件中,包含有 5 位学生的信息。 (The example file includes 5 students’ information.)
如第一行是学生“ Harry POTTER ”,其学号为“ 1766060101 ”,性别为“ m ”表示男性,专业为“ Computer Science and Technology”。第二行则是该学生所选课程的数量“ 4 ”,该数字表示接下来的 4 行分别列出了此学生选的 4 门 课程的编号,分别是第三行的“31060101 ”、第四行的“ 31060102 ”、第五行的“ 31060103 ”、第六行的“ 31060104 ”。 通过查课程文件 courses.txt 内容,我们知道该生选的课程名称分别是“ Academic English I ”、“ Advanced Mathematics
I1 ”、“ Introduction to IT ”和“ Programming Fundamental ”。(The first line in the file shows that the student’s name is “Harry POTTER”, his ID is “1766060101”, his gender is “m” that means male, his major is “Computer Science and Technology”.
The second line shows that the student selected “4” courses. In the following 4 lines, there are 4 courses’ IDs which present the 4 courses selected by the student, such as “31060101” in the 3rd line, “31060102” in the 4th line, “31060103” in the 5th line, and “31060104” in the 6th line. When we check the IDs in the file courses.txt, we know the courses the student selected are Academic English I, Advanced Mathematics I1, Introduction to IT, and Programming Fundamental.)
类似的,学生“ WuKong SUN ”选了两门课程,信息共占四行;学生“ XiaoTiao MA ” 选了 1 门课程,信息共 占 3 行;“ MuLan HUA ” 选了三门课程,信息共占五行;学生“ Bill GATES ” 选了 0 门课程,信息共占 2 行(由 于没选课程,所以选课程数“0 ”后没有课程编号对应的行)。
(Similarly, the student “WuKong SUN” selected “2” courses,
and the information includes 4 lines; The student “XiaoTiao MA” selected “1” course, and the information includes 3 lines; The student “MuLan HUA” selected “3” courses, and the information includes 5 lines. Student “Bill GATES” selected “0” course, and the information includes 2 lines because there is not any information about the selected course’s ID after the number of the selected courses, “0”.)
在本次实验中,需要对课程信息、学生信息进行维护。
(In this lab, you need to maintain the courses’ information and the students’ information.)
1 .课程信息的维护 (Management to the courses’ information)
某一门课程信息以列表进行存储,如下所示: (The information of some course is stored in a list, as shown below:)
["Academic English I","31060101","3","Liang ZHU-GE","8-102","Two hours each week-day."]
该列表 0 号下标的元素为课程名称; (the item in index 0 of the list is the course’s name;)
该列表 1 号下标的元素为课程编号; (the item in index 1 of the list is the course’s ID;)
该列表 2 号下标的元素为课程学分; (the item in index 2 of the list is the course’s credit;)
该列表 3 号下标的元素为课程授课教师; (the item in index 3 of the list is the course teacher’s name;)
该列表 4 号下标的元素为课程授课地点; (the item in index 4 of the list is the course’s address;)
该列表 5 号下标的元素为课程描述性信息。 (the item in index 5 of the list is about other information of the course.)
各门课程信息依次存入列表 courses_list 中,该列表的一项元素对应某门课程的信息。 (All courses information is stored in the list “courses_list” one by one, and one item of the list is corresponding to one course. )
维护课程信息时,提供以下操作: (When the courses’ information is maintained, your program need to supply the following operations:)
列出所有课程信息供查阅,对应的命令为“ show ”。 (List all courses’ information. The command is “show”.)
列出某一门课程信息供查阅(需要提供课程编号),对应的命令为“ search ”。特别注意:待查阅的课程编号 不存在时,应提示相应信息。
(List one course’s information according to the course’s ID. The command is “search”. Notice
that you should give some prompt message if the ID does not exist.)
增加一门课程,对应的命令为“ add ”,特别注意:当新增加的课程编号与已有课程编号重复时,应提示相应 信息。(Add a new course. The command is “add”. Notice that you should give some prompt message if the new course’s ID is same to the existed course’s.)
删除一门课程(需要提供课程编号),对应的命令为“ remove ”,特别注意:待删除的课程编号不存在时,应 提示相应信息。另外,还需注意,当某门课程被成功删除时,所有选择该课程的学生信息中的对应课程编号也应被 删除。(Delete one course according to the course’s ID. The command is “remove”. Notice that you should give some prompt message if the ID does not exist. Furthermore, when one course is deleted, all the course’s ID appearing in the students’ information should be deleted.)
修改某一门课程的信息(需要提供课程编号),注意被修改的信息不能是课程编号本身,对应的命令为“ update ”。
特别注意:待修改的课程编号不存在时,应提示相应信息。
(Modify one course’s information according to the course’s ID. The command is “update”. The course’s ID should not be modified. Notice that you should give some prompt message if
the ID does not exist.)
2 .学生信息的维护 (Management to the students’ information)
某一位学生信息以列表进行存储,如下所示: (The information of some student is stored in a list, as shown below:)
["WuKong SUN","1766060102","m","Computer Science and Technology",2,
["31060103","31060104"]]
该列表 0 号下标的元素为学生名称; (the item in index 0 of the list is the student’s name;)
该列表 1 号下标的元素为学生编号; (the item in index 1 of the list is the student’s ID;)
该列表 2 号下标的元素为学生性别; (the item in index 2 of the list is the student’s gender;)
该列表 3 号下标的元素为学生专业; (the item in index 3 of the list is the student’s major;)
该列表 4 号下标的元素为学生已选课程数目;
(the item in index 4 of the list is the number of the courses enrolled by
the student;)
该列表 5 号下标的元素为学生已选课程的列表。 (the item in index 5 of the list is the list of the courses enrolled by the student.)
各位学生信息依次存入列表 students_list 中,该列表的每一项元素对应某位学生的信息。
(All students information is stored in the list “students_list” one by one, and one item of the list is corresponding to one student. )
维护学生信息时,提供以下操作: (When the students’ information is maintained, your program need to supply the following operations:)
列出所有学生信息供查阅,对应的命令为“ display ”。 (List all the students’ information. The command is “display”.)
列出某一位学生信息供查阅(需要提供学生学号),对应的命令为“ retrieve ”。特别注意:待查阅的学生学号 不存在时,应提示相应信息。(List one student’s information according to the student’s ID. The command is “retrieve”.Notice that you should give some prompt message if the ID does not exist.)
增加一位学生,对应的命令为“ insert ”,特别注意:当新增加的学生学号与已有学生学号重复时,应提示相应 信息。(Add a new student. The command is “insert”. Notice that you should give some prompt message if the new student’s ID is same to the existed student’s.)
删除一位学生(需要提供学生学号),对应的命令为“ delete ”,特别注意:待删除的学生学号不存在时,应提示相应信息。
(Delete one student according to the student’s ID. The command is “delete”. Notice that you should give some prompt message if the ID does not exist. )
修改某一位学生的信息(需要提供学生学号),注意被修改的信息不能是学生学号本身,对应的命令为“ modify ”。
特别注意:待修改的学生学号不存在时,应提示相应信息。 (Modify one student’s information according to the student’s ID. The command is “modify”. The student’s ID should not be modified. Notice that you should give some prompt message if the ID does not exist.)
当待修改学生学号存在时,显示以下三种子命令供用户选择: (When the ID of the student whose information you want to modify exists, you should supply the following three sub-commands for user.)
子命令“ major ”的功能是,修改该生专业。 (The function of the sub-command “major” is to modify the student’s major.)
子命令“ enroll ”的功能是,为该生增加一门选修的课程。特别注意:当输入的选修课程编号不存在时,应提示 相应信息;当输入的选修课程编号与该生已修课程重复时,应提示相应信息。(The function of the sub-command “enroll” is to add a new course the student wants to select. Notice that you should give some prompt message if the course’s ID does not exist, and give other prompt message if the course’s ID is same to that the student has selected.)
子命令“ drop ”的功能是,为该生退掉一门选修的课程。特别注意:当输入的选修课程编号不存在时,应提示 相应信息;当输入的选修课程编号不在该生已修课程中时,应提示相应信息。
(The function of the sub-command “drop” is to drop a course the student has selected. Notice that you should give some prompt message if the course’s ID does not exist, and give other prompt message if the course’s ID is not in the student’s selected courses list.)
实验任务( Lab Tasks
完成 program.py 模块中的各相关函数。 (Please implement the functions in the module program.py.) main 函数首先定义两个空列表:课程信息列表 courses_list 和学生信息列表 students_list 。之后将对应两个文件 courses.txt 和 students.txt 的内容读到列表 courses_list students_list 中,注意两个列表的元素分别是某一门课程信息和某一位学生的信息。
(In the main() function, you should create 2 empty lists called courses_list and students_list firstly.
The courses_list is used to store courses’ information and the students_list is used to store students’ information. Then, you should read the contents of the files, courses.txt and students.txt, to the lists, courses_list and students_list, respectively.
Notice that the items of the two lists are the information of some course and some student, respectively.)
之后,程序显示如下的操作主菜单,提示用户进行三类操作。 (After that, the program should display the following main menu and prompt user to choose one command. The commands are grouped into 3 types.)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice:
第一类是维护课程信息的,五种命令上文已经讨论过。 (Type I is for maintaining courses’ information, the five
commands have been discussed as above.)
第二类是维护学生信息的,五种命令上文已经讨论过。 (Type II is for maintaining students’ information, the five commands have been discussed as above.)
第三类是退出命令(“ quit ”)。 (Type III is for terminating the program. The command is “quit”.)
用户在“ Please enter choice: ”提示之后输入对应的命令,完成相应的任务。当输入的命令不属于菜单中任何一条合法命令时,应该有提示信息指明输入的命令非法。(User can enter the corresponding command after the clue words, “Please enter choice:”, to fulfill a specific task. Notice that some prompt message should be displayed to specify the command is illegal if the entered command is wrong.)
当执行完用户的某一条命令后,系统再次显示主菜单,供用户选择新的任务加以执行。 (After executing the user’s command, the system should show the main menu again to wait for new commands. )
当用户选择退出命令时,需要调用写文件操作,将两个列表中的元素按照前文规定的文件格式写入两个新文件 “new_courses.txt ”、“ new_students.txt ”中。最后提示程序终止。 (If the command “quit” be selected, the system should call the operation writing to files, to write the items of the two lists (courses_list and students_list) to the new files (“new_courses.txt” and “new_students.txt”) respectively, according to the format set out in the above text. Finally, the system should prompt the program is terminated.)
请注意程序的代码风格应该遵守上课所讲的规则。 (Please note that the code style of the programs should comply with the rules mentioned in the class. )
运行示例参见附录。( See appendix for the sample output.)
附录:运行示例 (Appendix: Sample Output)
运行示例 01 (Sample Output 01)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: a
Not a valid command - please try again.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: show
========================================
All Courses Information
========================================
----------------------------------------
Course Name: Academic English I
Course ID | Credits: 31060101 | 3
Instructor Name: Liang ZHU-GE
Address: 8-102
Description: Two hours each week-day.
----------------------------------------
Course Name: Advanced Mathematics I1
Course ID | Credits: 31060102 | 4.5
Instructor Name: ChongZhi ZU
Address: 8-101
Description: Two lectures in a week.
----------------------------------------
Course Name: Introduction to IT
Course ID | Credits: 31060103 | 3.5
Instructor Name: ChuanLiang LIU
Address: 9-201
Description: The course includes 16 lab hours.
----------------------------------------
Course Name: Programming Fundamental
Course ID | Credits: 31060104 | 3.5
Instructor Name: Donald E. KNUTH
Address: 9-203
Description: The course includes 28 lab hours.
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: search
Please enter course ID: asd
Course (ID=asd) is not found in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: search
Please enter course ID: 31060103
Course Name: Introduction to IT
Course ID | Credits: 31060103 | 3.5
Instructor Name: ChuanLiang LIU
Address: 9-201
Description: The course includes 16 lab hours.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: quit
-- Program Terminating --
注意,程序运行之后,在当前目录下两个新文件“ new_courses.txt ”、“ new_students.txt ”内容分别如下: (Notice
that there are two new files, new_courses.txt and new_students.txt, in the current folder when the program finishes. Their contents are as follow.)
new_courses.txt
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week​​​​​​​
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.
Programming Fundamental,31060104,3.5,Donald E. KNUTH,9-203
The course includes 28 lab hours.
new_students.txt
Harry POTTER,1766060101,m,Computer Science and Technology
4
31060101
31060102
31060103
31060104
WuKong SUN,1766060102,m,Computer Science and Technology
2
31060103
31060104
XiaoTiao MA,1766060103,m,Computer Science and Technology
1
31060104
MuLan HUA,1766060104,f,Computer Science and Technology
3
31060102
31060103
31060104
Bill GATES,1766060105,m,Computer Science and Technology
0
运行示例 02 (Sample Output 02)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: add
Please enter course ID: 31060103
Course (ID=31060103) already exists in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: add​​​​​​​
Please enter course ID: 31060166
Please input course name: Java Programming
Please input course credits: 3.5
Please input course instructor: Si LI
Please input course address: 8-201
Please input course description: The course includes 40 lecture hours and 32 lab hours.
Successfully added the Course (ID=31060166) to the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: search
Please enter course ID: 31060166
Course Name: Java Programming
Course ID | Credits: 31060166 | 3.5
Instructor Name: Si LI
Address: 8-201
Description: The course includes 40 lecture hours and 32 lab hours.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: update
Please enter course ID: sss
Course (ID=sss) is not found in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: update
Please enter course ID: 31060166
Before updating, the course information:
----------------------------------------
Course Name: Java Programming
Course ID | Credits: 31060166 | 3.5
Instructor Name: Si LI​​​​​​​
Address: 8-201
Description: The course includes 40 lecture hours and 32 lab hours.
----------------------------------------
Please enter course name: Introduction to Java Programming
Please enter course credits: 3.0
Please enter course instructor: Wu WANG
Please enter course address: 8-202
Please enter course description: The course includes 28 lecture hours and 20 lab hours.
Successfully updated the Course (ID=31060166) to the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: search
Please enter course ID: 31060166
Course Name: Introduction to Java Programming
Course ID | Credits: 31060166 | 3.0
Instructor Name: Wu WANG
Address: 8-202
Description: The course includes 28 lecture hours and 20 lab hours.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: quit
-- Program Terminating –
注意,程序运行之后,在当前目录下两个新文件“ new_courses.txt ”、“ new_students.txt ”内容分别如下: (Notice that there are two new files, new_courses.txt and new_students.txt, in the current folder when the program finishes. Their contents are as follow.)
new_courses.txt
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week.
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.​​​​​​​
Programming Fundamental,31060104,3.5,Donald E. KNUTH,9-203
The course includes 28 lab hours.
Introduction to Java Programming,31060166,3.0,Wu WANG,8-202
The course includes 28 lecture hours and 20 lab hours.
new_students.txt
Harry POTTER,1766060101,m,Computer Science and Technology
4
31060101
31060102
31060103
31060104
WuKong SUN,1766060102,m,Computer Science and Technology
2
31060103
31060104
XiaoTiao MA,1766060103,m,Computer Science and Technology
1
31060104
MuLan HUA,1766060104,f,Computer Science and Technology
3
31060102
31060103
31060104
Bill GATES,1766060105,m,Computer Science and Technology
0
运行示例 03 (Sample Output 03)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: display
========================================
All Students Information
========================================
========================================
----------------------------------------
Student Name: Harry POTTER
Student ID (Gender): 1766060101 (m)
Major: Computer Science and Technology
Selected Courses (4) :
31060101: Academic English I​​​​​​​
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: WuKong SUN
Student ID (Gender): 1766060102 (m)
Major: Computer Science and Technology
Selected Courses (2) :
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: XiaoTiao MA
Student ID (Gender): 1766060103 (m)
Major: Computer Science and Technology
Selected Courses (1) :
31060104: Programming Fundamental
----------------------------------------
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (3) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: Bill GATES
Student ID (Gender): 1766060105 (m)
Major: Computer Science and Technology
Selected Courses (0) :
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: retrieve
Please enter student ID: aaa
Student (ID=aaa) is not found in the students list.
 
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: retrieve
Please enter student ID: 1766060104
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (3) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: insert
Please enter student ID: 1766060104
Student (ID=1766060104) already exists in the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: insert
Please enter student ID: 1766060188
Please enter student name: De AN
Please enter student gender(m/f): a
Please enter student gender(m/f): x
Please enter student gender(m/f): f
Please enter student major: Software Engineering
Successfully inserted the Student (ID=1766060188) into the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]​​​​​​​
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: retrieve
Please enter student ID: 1766060188
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Software Engineering
Selected Courses (0) :
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: sss
Student (ID=sss) is not found in the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: qwert
Not a valid command - returning to main menu.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: major
Please enter student major: Computer Engineering​​​​​​​
Modified the major of Student (ID=1766060188).
----------------------------------------
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Computer Engineering
Selected Courses (0) :
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: enroll
Please enter ID of course to be enrolled in: zxcvb
Course (ID=zxcvb) is not found in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: enroll
Please enter ID of course to be enrolled in: 31060103
Modified the information of Student (ID=1766060188).
----------------------------------------
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Computer Engineering
Selected Courses (1) :
31060103: Introduction to IT
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]​​​​​​​
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 31060103
Student (ID=31060103) is not found in the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: enroll
Please enter ID of course to be enrolled in: 31060103
Course (ID=31060103) is already in the enrolled courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: enroll
Please enter ID of course to be enrolled in: 31060104
Modified the information of Student (ID=1766060188).
----------------------------------------
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Computer Engineering
Selected Courses (2) :
31060103: Introduction to IT
31060104: Programming Fundamental
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]​​​​​​​
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: drop
Please enter ID of course to be dropped: dfgh
Course (ID=dfgh) is not found in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: modify
Please enter student ID: 1766060188
Modify student (ID=%s) [major|enroll|drop]: drop
Please enter ID of course to be dropped: 31060103
Modified the information of Student (ID=1766060188).
----------------------------------------
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Computer Engineering
Selected Courses (1) :
31060104: Programming Fundamental
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: display
========================================
All Students Information
========================================
========================================
----------------------------------------
Student Name: Harry POTTER
Student ID (Gender): 1766060101 (m)
Major: Computer Science and Technology​​​​​​​
Selected Courses (4) :
31060101: Academic English I
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: WuKong SUN
Student ID (Gender): 1766060102 (m)
Major: Computer Science and Technology
Selected Courses (2) :
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: XiaoTiao MA
Student ID (Gender): 1766060103 (m)
Major: Computer Science and Technology
Selected Courses (1) :
31060104: Programming Fundamental
----------------------------------------
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (3) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: Bill GATES
Student ID (Gender): 1766060105 (m)
Major: Computer Science and Technology
Selected Courses (0) :
----------------------------------------
Student Name: De AN
Student ID (Gender): 1766060188 (f)
Major: Computer Engineering
Selected Courses (1) :
31060104: Programming Fundamental
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]​​​​​​​
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: quit
-- Program Terminating –
注意,程序运行之后,在当前目录下两个新文件“ new_courses.txt ”、“ new_students.txt ”内容分别如下: (Notice that there are two new files, new_courses.txt and new_students.txt, in the current folder when the program finishes. Their contents are as follow.)
new_courses.txt
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week.
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.
Programming Fundamental,31060104,3.5,Donald E. KNUTH,9-203
The course includes 28 lab hours.
new_students.txt
Harry POTTER,1766060101,m,Computer Science and Technology
4
31060101
31060102
31060103
31060104
WuKong SUN,1766060102,m,Computer Science and Technology
2
31060103
31060104
XiaoTiao MA,1766060103,m,Computer Science and Technology
1
31060104
MuLan HUA,1766060104,f,Computer Science and Technology
3
31060102
31060103
31060104
Bill GATES,1766060105,m,Computer Science and Technology
0
De AN,1766060188,f,Computer Engineering
1
31060104
 
运行示例 04 (Sample Output 04)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: display
========================================
All Students Information
========================================
========================================
----------------------------------------
Student Name: Harry POTTER
Student ID (Gender): 1766060101 (m)
Major: Computer Science and Technology
Selected Courses (4) :
31060101: Academic English I
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: WuKong SUN
Student ID (Gender): 1766060102 (m)
Major: Computer Science and Technology
Selected Courses (2) :
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: XiaoTiao MA
Student ID (Gender): 1766060103 (m)
Major: Computer Science and Technology
Selected Courses (1) :
31060104: Programming Fundamental
----------------------------------------
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (3) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental​​​​​​​
----------------------------------------
Student Name: Bill GATES
Student ID (Gender): 1766060105 (m)
Major: Computer Science and Technology
Selected Courses (0) :
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: remove
Please enter course ID: ssss
Course (ID=ssss) is not found in the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: remove
Please enter course ID: 31060104
Successfully removed the Course (ID=31060104) from the courses list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: display
========================================
All Students Information
========================================
========================================
----------------------------------------
Student Name: Harry POTTER
Student ID (Gender): 1766060101 (m)
Major: Computer Science and Technology​​​​​​​
Selected Courses (3) :
31060101: Academic English I
31060102: Advanced Mathematics I1
31060103: Introduction to IT
----------------------------------------
Student Name: WuKong SUN
Student ID (Gender): 1766060102 (m)
Major: Computer Science and Technology
Selected Courses (1) :
31060103: Introduction to IT
----------------------------------------
Student Name: XiaoTiao MA
Student ID (Gender): 1766060103 (m)
Major: Computer Science and Technology
Selected Courses (0) :
----------------------------------------
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (2) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
----------------------------------------
Student Name: Bill GATES
Student ID (Gender): 1766060105 (m)
Major: Computer Science and Technology
Selected Courses (0) :
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: show
========================================
All Courses Information
========================================
----------------------------------------
Course Name: Academic English I
Course ID | Credits: 31060101 | 3​​​​​​​
Instructor Name: Liang ZHU-GE
Address: 8-102
Description: Two hours each week-day.
----------------------------------------
Course Name: Advanced Mathematics I1
Course ID | Credits: 31060102 | 4.5
Instructor Name: ChongZhi ZU
Address: 8-101
Description: Two lectures in a week.
----------------------------------------
Course Name: Introduction to IT
Course ID | Credits: 31060103 | 3.5
Instructor Name: ChuanLiang LIU
Address: 9-201
Description: The course includes 16 lab hours.
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: quit
-- Program Terminating --
注意,程序运行之后,在当前目录下两个新文件“ new_courses.txt ”、“ new_students.txt ”内容分别如下: (Notice that there are two new files, new_courses.txt and new_students.txt, in the current folder when the program finishes. Their contents are as follow.)
new_courses.txt
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week.
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.
new_students.txt
Harry POTTER,1766060101,m,Computer Science and Technology
3
31060101
31060102​​​​​​​
31060103
WuKong SUN,1766060102,m,Computer Science and Technology
1
31060103
XiaoTiao MA,1766060103,m,Computer Science and Technology
0
MuLan HUA,1766060104,f,Computer Science and Technology
2
31060102
31060103
Bill GATES,1766060105,m,Computer Science and Technology
0
运行示例 05 (Sample Output 05)
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: delete
Please enter student ID: adsdd
Student (ID=adsdd) is not found in the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: delete
Please enter student ID: 1766060105
Successfully deleted the Student (ID=1766060105) from the students list.
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: display
========================================
All Students Information
========================================
========================================​​​​​​​
----------------------------------------
Student Name: Harry POTTER
Student ID (Gender): 1766060101 (m)
Major: Computer Science and Technology
Selected Courses (4) :
31060101: Academic English I
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: WuKong SUN
Student ID (Gender): 1766060102 (m)
Major: Computer Science and Technology
Selected Courses (2) :
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
Student Name: XiaoTiao MA
Student ID (Gender): 1766060103 (m)
Major: Computer Science and Technology
Selected Courses (1) :
31060104: Programming Fundamental
----------------------------------------
Student Name: MuLan HUA
Student ID (Gender): 1766060104 (f)
Major: Computer Science and Technology
Selected Courses (3) :
31060102: Advanced Mathematics I1
31060103: Introduction to IT
31060104: Programming Fundamental
----------------------------------------
========================================
-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: retrieve
Please enter student ID: 1766060105

Student (ID=1766060105) is not found in the students list.​​​​​​​

-------------------------------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]
Please enter choice: quit
-- Program Terminating –
注意,程序运行之后,在当前目录下两个新文件“ new_courses.txt ”、“ new_students.txt ”内容分别如下: (Notice that there are two new files, new_courses.txt and new_students.txt, in the current folder when the program finishes. Their contents are as follow.)
new_courses.txt
Academic English I,31060101,3,Liang ZHU-GE,8-102
Two hours each week-day.
Advanced Mathematics I1,31060102,4.5,ChongZhi ZU,8-101
Two lectures in a week.
Introduction to IT,31060103,3.5,ChuanLiang LIU,9-201
The course includes 16 lab hours.
Programming Fundamental,31060104,3.5,Donald E. KNUTH,9-203
The course includes 28 lab hours.
new_students.txt
Harry POTTER,1766060101,m,Computer Science and Technology
4
31060101
31060102
31060103
31060104
WuKong SUN,1766060102,m,Computer Science and Technology
2
31060103
31060104
XiaoTiao MA,1766060103,m,Computer Science and Technology
1
31060104
MuLan HUA,1766060104,f,Computer Science and Technology
3
31060102
31060103
31060104

实现的代码如下:

需要在同一个文件夹中放有courses.txt文件和students.txt文件,以及下列的代码文件。

def students_txt_to_list():
    students_list=[]
    with open("students.txt", "r") as file:
        #每次读取一个学生的信息,并加入总列表
        while True:
            # 存储单个学生的信息list
            signal_student_list = []

            line=file.readline().strip()
            if(line!=""):
                tmp=line.split(",")
                courses_num=int(file.readline().strip())
                tmp.append(str(courses_num))
                courses_con=[]
                for i in range(courses_num):
                    lines=file.readline().strip()
                    courses_con.append(lines)
                signal_student_list=tmp+courses_con
                students_list.append(signal_student_list)
            else:
                break
    return students_list

# 从列表写入学生信息到txt
def students_list_to_newTxt(students_list):
    with open("new_students.txt", "w") as file:
        for i in range(len(students_list)):
            str_result=""
            str_result+=students_list[i][0]+","+students_list[i][1]+","+students_list[i][2]+","+students_list[i][3]+"\n"
            str_result+=str(len(students_list[i][5:]))+"\n"
            for one in students_list[i][5:]:
                str_result+=one+"\n"
        
            file.write(str_result)
        file.flush()
    # 删除最后一行的空行

# 读取课程信息到列表
def courses_txt_to_list():
    courses_list = []
    with open("courses.txt", "r") as file:
        # 每次读取一个课程的信息,并加入总列表
        while True:
            # 存储单个课程的信息list
            signal_course_list = []
            line = file.readline().strip()
            if (line != ""):
                tmp = line.split(',')
                tmp2 = [file.readline().strip()]
                signal_course_list=tmp+tmp2
                courses_list.append(signal_course_list)
            else:
                break
    return courses_list


# 从列表写入课程信息到txt
def courses_list_to_newTxt(courses_list):
    with open("new_courses.txt", "w") as file:
        for i in courses_list:
            file.write(i[0])
            file.write(',')
            file.write(i[1])
            file.write(',')
            file.write(i[2])
            file.write(',')
            file.write(i[3])
            file.write(',')
            file.write(i[4])
            file.write('\n')
            file.write(i[5])
            file.write('\n')

if __name__ == '__main__':
    courses_list= courses_txt_to_list()
    students_list= students_txt_to_list()
    while True:
       opt = input("""-----------------------------------------------------------------------
The choice to maintain COURSEs information [add|remove|update|search|show]
The choice to maintain STUDENTs information [insert|delete|modify|retrieve|display]
The choice quit means to finish the program.[quit]


Please enter choice: """)
       if opt=='add':
          cid=input('Please input course ID:')
          flagxx=-1
          for i in range(len(courses_list)):
                if (courses_list[i][1]==cid):
                        print('Course (ID=%s) already exists in the courses list.'%(cid))
                        flagxx=1
          if flagxx!=1:
            new_course_list=[input('Please input course name:'),cid,input('Please input course credits:'),input('Please input course instructor:'),input('Please input course address:'),input('Please input course description:')]
            courses_list.append(new_course_list)                   
            print('Successfully added the Course (ID=%s) to the courses list.'%(cid))
            continue   
       if(opt=='remove'):
            cid=input('Please enter course ID:')

            flagxx=-1
            for i0 in courses_list:
                if(cid in i0):
                # 删除该门课程
                    flag1 = -1
                    for i in range(len(courses_list)):
                        if (courses_list[i][1] == cid):
                            flag1 = i
                    courses_list.pop(flag1)
                # 删除学生选课中的记录
                    flag2=-1
                    for x in range(len(students_list)):
                        for i in range(len(students_list[x])):
                            if (students_list[x][i] == cid):
                                flag2=i
                        if(flag2!=-1):
                            students_list[x].pop(flag2)
                            students_list[x][4]=str(int(students_list[x][4])-1)
                            flag2=-1
                    flagxx=1
                    print('Successfully removed the Course (ID=%s) from the courses list.'%(cid))
            if(flagxx!=1):
                print('Course (ID=%s) is not found in the courses list.'%(cid))
                continue
               
       if(opt=='update'):
            cid = input('Please enter course ID:')
            flagxx=-1
            for i in range(len(courses_list)):
                if(courses_list[i][1]==cid):
                    print("Before updating,the course information:")
                    print("--------------------------------------")
                    print("Course name:",courses_list[i][0])
                    print("Course ID|Credits:",courses_list[i][1],'|',courses_list[i][2])
                    print("Instructor Name:",courses_list[i][3])
                    print("Address:",courses_list[i][4])
                    print("Description:",courses_list[i][5])
                    print("--------------------------------------")
                    courses_list[i][0]=input('Please enter course name:')
                    courses_list[i][2]=input('Please enter course credits:')
                    courses_list[i][3]=input('Please enter course instructor:')
                    courses_list[i][4]=input('Please enter course address:')
                    courses_list[i][5]=input('Please enter course descriprion:')
                    print('Successfully updated the Course (ID=%s) to the courses list.'%(cid))
                    flagxx=1
            if (flagxx != 1):
                print('Course (ID=%s) is not found in the courses list.'%(cid))
                continue

       if(opt=='search'):
            cid = input('Please enter course ID:')
            flagxx = -1
            for i in range(len(courses_list)):
                if (courses_list[i][1] == cid):
                    print("Course name:",courses_list[i][0])
                    print("Course ID|Credits:",courses_list[i][1],'|',courses_list[i][2])
                    print("Instructor Name:",courses_list[i][3])
                    print("Address:",courses_list[i][4])
                    print("Description:",courses_list[i][5])
                    flagxx = 1
            if(flagxx != 1):
                print('Course (ID=%s) is not found in the courses list.'%(cid))
            continue
       
       if(opt=='show'):
            print("=========================================")
            print("All Courses Information")
            print("=========================================")
            print("-----------------------------------------")
            for i in courses_list:
                print("Course Name:",i[0])
                print("Course ID:",i[1]+"|"+i[2])
                print("Instructor Name:",i[3])
                print("Address:",i[4])
                print("Description:",i[5])
                print("-----------------------------------------")
            print("=========================================")
            continue
       
       if opt=='insert':
            sid = input('Please enter student ID:')
            flagxx=-1
            for i in range(len(students_list)):
                if students_list[i][1]==sid:
                    print('Student(ID=%s) already exists in the students list.'%(sid))
                    flagxx=1
            if flagxx!=1:
                sname=input('Please enter student name:')
                sgender=input('Please enter student gender(m/f):')
                while sgender!='m'and sgender!='f':
                    sgender=input('Please enter student gender(m/f):')
                new_student_list=[sname,sid,sgender,input("Please enter student major:"),0]
                students_list.append(new_student_list)
                print('Successfully inserted the Student (ID=%s) into the students list.'%(sid))
                continue
                    
        #删除学生
       if(opt=='delete'):
            sid = input('Please enter student ID:')

            flagxx = -1
            for i0 in students_list:
                if (sid in i0):
                    # 删除该学生
                    flag1 = -1
                    for i in range(len(students_list)):
                        if (students_list[i][1] == sid):
                            flag1 = i
                    students_list.pop(flag1)
                    flagxx = 1
                    print('Successfully deleted the Student (ID=%s) from the students list.'%(sid))
            if (flagxx != 1):
                print('Student (ID=%s) is not found in the students list.'%(sid))
            continue
       
       if(opt=='modify'):
            sid = input("Please enter student ID:")    
            flagxx = -1
            for i in range(len(students_list)):
                if (students_list[i][1] == sid):
                    choice=input("Modify student(ID=%s) [magor|enroll|drop]:")
                    flagxx=1
                    if choice!='major'and choice!='enroll'and choice!='drop':
                        print()
                        print("Not a valid command-returning to main menu.")
                    if choice=='major':
                        smajor=input('Please enter student major:')
                        students_list[i][3]=smajor
                        print("Modified the major of Student(ID=%s)."%(sid))
                        print()
                        print('---------------------------------------------')
                        print("Student Name:",students_list[i][0])
                        print("student ID|(Gender):",students_list[i][1]+"("+students_list[i][2]+")")
                        print("Major:",students_list[i][3])
                        print("Selected Courses"+"("+str(students_list[i][4])+"):")
                        flagx=-1
                        for a in students_list[i][5:]:
                            for p in range(len(courses_list)):
                                if (courses_list[p][1]==a):
                                    print("    "+a+":"+courses_list[p][0])
                        print("---------------------------------------------"+"\n\n")
                        flagx=1
                        continue

                    if choice=='enroll':
                        
                        senroll=input("Please enter ID of course to be enrolled in:")
                        flagxxxx=-1
                        if senroll in students_list[i][5:]:
                            print("Course (ID=%s) is already in the enrolled courses list." % (senroll))
                            flagxxxx=1
                        if(flagxxxx!=1):
                            flagxxxxxx=-1
                            for x in range(len(courses_list)):
                                if senroll==courses_list[x][1]:
                                    students_list[i][4]=int(students_list[i][4])+1                                    
                                    students_list[i].append(senroll)
                                    print()
                                    print("Modified the information of Student(ID=%s)."%(sid))
                                    print("---------------------------------------------")
                                    print("Student Name:",students_list[i][0])
                                    print("student ID|(Gender):",students_list[i][1]+"("+students_list[i][2]+")")
                                    print("Major:",students_list[i][3])
                                    print("Selected Courses"+"("+str(students_list[i][4])+"):")
                                    for a in students_list[i][5:]:
                                        for p in range(len(courses_list)):
                                            if (courses_list[p][1]==a):
                                                print("    "+a+":"+courses_list[p][0])
                                    print("---------------------------------------------"+"\n\n")
                                    flagxxxxxx=1
                            if (flagxxxxxx!=1):
                                print("Course(ID=%s) is not found in the courses list."%(senroll))
                                continue
                    
                    if choice=='drop':
                        flagxxxxxxx=-1
                        sdrop=input("Please enter ID of course to be dropped:")
                        for a in range(len(courses_list)):
                            if sdrop==courses_list[a][1] and sdrop in students_list[i][5:]:
                                students_list[i][4]=int(students_list[i][4])-1
                                students_list[i].remove(sdrop)
                                
                                print("Modified the information of Student(ID=%s)." % sid)
                                print("---------------------------------------------")
                                print()
                                print("Student Name:",students_list[i][0])
                                print("student ID|(Gender):",students_list[i][1]+"("+students_list[i][2]+")")
                                print("Major:",students_list[i][3])
                                print("Selected Courses"+"("+str(students_list[i][4])+"):")
                                for a in students_list[i][5:]:
                                    for p in range(len(courses_list)):
                                        if (courses_list[p][1]==a):
                                            print("    "+a+":"+courses_list[p][0])
                                print("---------------------------------------------"+"\n\n")
                                flagxxxxxxx=1
                                
                        if flagxxxxxxx!=1:
                            print("Course (ID=%s) is not found in the courses list." % sdrop)
                        continue        
            if (flagxx)!=1:
                print("Student (ID=%s) is not found in the students list." % sid)
                continue

       if(opt=='retrieve'):
            sid = input('Please enter student ID:')
            print()
            print()
            flagxx = -1
            for i in range(len(students_list)):
                if (students_list[i][1] == sid):
                    print("Student Name:",students_list[i][0])
                    print("student ID|(Gender):",students_list[i][1]+"("+students_list[i][2]+")")
                    print("Major:",students_list[i][3])
                    print("Selected Courses","(",students_list[i][4],"):")
                    for a in students_list[i][5:]:
                        for p in range(len(courses_list)):
                            if (courses_list[p][1]==a):
                                print("    "+a+":"+courses_list[p][0])
                    flagxx = 1
            if (flagxx != 1):
                print('Student (ID=%s) is not found in the students list'%(sid))
            continue
        
       if(opt=='display'):
            print("All students Infotmation")
            for i in students_list:
                print("================================")
                print("Student Name:",i[0])
                print("student ID|(Gender):"+i[1]+"("+i[2]+")")
                print("Major:",i[3])
                print("Selected Courses"+"""("""+(str(i[4]))+""")"""+ ':')
                for a in i[5:]:
                    for p in range(len(courses_list)):
                        if (courses_list[p][1] == a):
                            print("    "+a+":"+courses_list[p][0])    
                continue

       if(opt=='quit'):
            print('-- Program Terminating --')
            courses_list_to_newTxt(courses_list)
            students_list_to_newTxt(students_list)
            break
       
       if(opt!='add' and opt!='remove' and opt!='update' and opt!='search' and opt!='show' and opt!='insert' and opt!='delete' and opt!='modify' and opt!='retrieve' and opt!='display'):
            print('Not a valid command-please try again.')
       else:
           pass
    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值