-4

我必须创建一个程序,要求用户输入学生的详细信息,包括他们的 ID 号(将保存到全局列表中)。然后用户将返回到主菜单,要求他们查看添加或删除。

我可以添加一个学生,我已经完成了视图,我已经创建了班级等等......但我无法删除它。我们必须通过输入他们的 ID 号来删除我们添加的学生,然后在删除后程序必须确认它删除了谁。

def removeStudent():
    std_delete = raw_input("Please enter Students ID number to remove: ")
    for std in students:
        if std.studentID == std_delete:
            print "Student ID",std_delete,"has been deleted" % std.studentID
            students.remove(std)
            break
4

2 回答 2

0

这一定是您要查找的代码。

def delete_student(id):
    for student in students:
        if student.studentID == id:
           index = students.index(student)
           break
    print "Deleted student %s" % students.pop(index)
于 2012-12-16T10:36:35.533 回答
0

我认为您对如何删除学生实例感到困惑

std_delete = raw_input("Student To Delete")
for std in students:
    if std.studentID == std_delete:
        print "Student ID %d has been deleted " % std.studentID
        students.remove(std)
        break

或者你可以使用过滤功能

std_delete = raw_input("Student To Delete")
students = filter(lambda x: x.studentID != std_delete, students)
于 2012-12-16T10:39:02.303 回答