使用pathon制作一个学生选课系统
时间: 2024-01-22 19:19:44 浏览: 31
好的,我可以帮您介绍一下Python实现学生选课系统的基本思路。
1. 确定数据结构
首先,我们需要确定学生、课程、选课记录等数据的结构。在Python中可以使用字典来存储这些数据,例如:
```python
students = {
"s001": {"name": "Tom", "courses": []},
"s002": {"name": "Jerry", "courses": []}
}
courses = {
"c001": {"name": "Math", "capacity": 50, "enrolled": []},
"c002": {"name": "English", "capacity": 40, "enrolled": []}
}
records = []
```
2. 编写功能函数
接下来,我们需要编写一些函数来实现选课系统的基本功能,如添加学生、添加课程、选课、退课等。例如:
```python
def add_student(student_id, name):
students[student_id] = {"name": name, "courses": []}
def add_course(course_id, name, capacity):
courses[course_id] = {"name": name, "capacity": capacity, "enrolled": []}
def enroll(student_id, course_id):
student = students.get(student_id)
course = courses.get(course_id)
if student and course:
if len(course["enrolled"]) < course["capacity"]:
student["courses"].append(course_id)
course["enrolled"].append(student_id)
records.append({"student_id": student_id, "course_id": course_id, "action": "enroll"})
print("Enroll success!")
else:
print("Course is full!")
else:
print("Invalid student ID or course ID!")
def withdraw(student_id, course_id):
student = students.get(student_id)
course = courses.get(course_id)
if student and course:
if course["enrolled"].count(student_id) > 0:
student["courses"].remove(course_id)
course["enrolled"].remove(student_id)
records.append({"student_id": student_id, "course_id": course_id, "action": "withdraw"})
print("Withdraw success!")
else:
print("Student has not enrolled this course!")
else:
print("Invalid student ID or course ID!")
```
3. 编写主程序
最后,我们可以编写一个主程序来调用这些功能函数,实现完整的选课系统。例如:
```python
while True:
print("1. Add student")
print("2. Add course")
print("3. Enroll course")
print("4. Withdraw course")
print("5. Quit")
choice = input("Enter your choice: ")
if choice == "1":
student_id = input("Enter student ID: ")
name = input("Enter student name: ")
add_student(student_id, name)
elif choice == "2":
course_id = input("Enter course ID: ")
name = input("Enter course name: ")
capacity = int(input("Enter course capacity: "))
add_course(course_id, name, capacity)
elif choice == "3":
student_id = input("Enter student ID: ")
course_id = input("Enter course ID: ")
enroll(student_id, course_id)
elif choice == "4":
student_id = input("Enter student ID: ")
course_id = input("Enter course ID: ")
withdraw(student_id, course_id)
elif choice == "5":
break
else:
print("Invalid choice!")
```
以上是一个简单的学生选课系统的Python实现思路,您可以根据自己的需求和实际情况进行修改和完善。