OOP - 创建类添加到列表然后删除

时间:2012-12-16 10:29:53

标签: python

我必须创建一个程序,要求用户输入学生的详细信息,包括他们的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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)