写一个Python程序,学生选课系统,代码要包含序列、字典、模块、类、第三方模块的安装与使用等方面
时间: 2023-07-10 17:29:47 浏览: 85
好的,我可以为您编写一个学生选课系统的Python程序,以下是代码:
首先,我们需要安装一个第三方模块,用于生成唯一的学生ID。可以使用uuid模块,通过以下命令安装:
```python
pip install uuid
```
然后,我们需要创建一个模块student,包含一个Student类和一个students字典,用于保存所有学生信息。代码如下:
```python
import uuid
class Student:
def __init__(self, name, age, gender):
self.id = str(uuid.uuid4())
self.name = name
self.age = age
self.gender = gender
self.courses = []
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
def __str__(self):
return f"ID: {self.id}\nName: {self.name}\nAge: {self.age}\nGender: {self.gender}\nCourses: {', '.join(self.courses)}"
students = {}
```
接下来,我们需要创建一个模块course,包含一个Course类和一个courses列表,用于保存所有课程信息。代码如下:
```python
class Course:
def __init__(self, name, teacher):
self.name = name
self.teacher = teacher
self.students = []
def add_student(self, student):
self.students.append(student)
student.add_course(self.name)
def remove_student(self, student):
self.students.remove(student)
student.remove_course(self.name)
def __str__(self):
return f"Name: {self.name}\nTeacher: {self.teacher}\nStudents: {[student.name for student in self.students]}"
courses = []
```
然后,我们需要创建一个main模块,用于实现用户交互界面。代码如下:
```python
from student import Student, students
from course import Course, courses
def print_menu():
print("=" * 30)
print("1. Add student")
print("2. View student info")
print("3. Add course")
print("4. View course info")
print("5. Enroll student in course")
print("6. Withdraw student from course")
print("7. Exit")
print("=" * 30)
def add_student():
name = input("Enter student name: ")
age = input("Enter student age: ")
gender = input("Enter student gender: ")
student = Student(name, age, gender)
students[student.id] = student
print(f"Student with ID {student.id} added.")
def view_student_info():
id = input("Enter student ID: ")
student = students.get(id)
if student:
print(student)
else:
print(f"Student with ID {id} not found.")
def add_course():
name = input("Enter course name: ")
teacher = input("Enter teacher name: ")
course = Course(name, teacher)
courses.append(course)
print(f"Course {name} added.")
def view_course_info():
name = input("Enter course name: ")
for course in courses:
if course.name == name:
print(course)
break
else:
print(f"Course {name} not found.")
def enroll_student():
id = input("Enter student ID: ")
student = students.get(id)
if not student:
print(f"Student with ID {id} not found.")
return
name = input("Enter course name: ")
for course in courses:
if course.name == name:
course.add_student(student)
print(f"Student {student.name} enrolled in course {name}.")
break
else:
print(f"Course {name} not found.")
def withdraw_student():
id = input("Enter student ID: ")
student = students.get(id)
if not student:
print(f"Student with ID {id} not found.")
return
name = input("Enter course name: ")
for course in courses:
if course.name == name:
course.remove_student(student)
print(f"Student {student.name} withdrawn from course {name}.")
break
else:
print(f"Course {name} not found.")
def main():
while True:
print_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_student()
elif choice == "2":
view_student_info()
elif choice == "3":
add_course()
elif choice == "4":
view_course_info()
elif choice == "5":
enroll_student()
elif choice == "6":
withdraw_student()
elif choice == "7":
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
```
运行程序后,用户可以通过菜单选择添加学生、查看学生信息、添加课程、查看课程信息、将学生注册到课程中以及将学生从课程中退出等操作。所有学生和课程信息都保存在students和courses中。
阅读全文