用python编写学生管理系统 -v1

#该程序在设计返回值时用了flag(标志)和i(标识下标);
#之前在travers()函数的编写中for循环中缺少一个return导致在传递参数时会有错误:
# 主要体现在修改和删除首个元素时!值得警醒
students = []
name = ""
sex = ""
stuID = ""


def traverse(num):
    flag = 0
    i=-1
    for i in range(len(students)):
        if students[i]['stuID'] == num:
            flag = 1
            return flag,i
    return flag,i


def selectStu():
    stunum = input("请输入要查询学生的学号:")
    f,n=traverse(stunum)
    if f==1:
            print("该同学存在")
    else:
        print("没有此同学,查询失败!")
    print()
    return f ,n



def insertStu():
    global name
    global sex
    global stuID
    number = input("请输入学生学号:")
    f,n=traverse(number)
    if f == 1:
            print("该同学存在,无需添加")
    if f == 0:
        inform = {}
        name = input("请输入学生姓名:")
        sex = input("请输入学生性别:")
        inform['name'] = name
        inform['sex'] = sex
        inform['stuID'] = number
        students.append(inform)
        print("学生添加成功!")
    print()




def deleteStu():
    n ,m = selectStu()
    if n == 1:
        students.remove(students[m])
    if n == 0:
        print("没有此学生,删除失败!")
    else:
        print("删除成功!")

    print()


def editStu():
    n ,m = selectStu()
    if n == 1:
        while True:
            num = input("请选择——1.修改姓名  2.修改性别  3.修改学号  4.退出修改\n")
            if num == '1':
                newname = input("输入更改后的姓名:")
                students[m]['name'] = newname
            elif num == '2':
                newsex = input("输入更改后的性别:")
                students[m]['sex'] = newsex
            elif num == '3':
                newID = input("输入更改后的学号:")
                students[m]['stuID'] = newID
            elif num == '4':
                break
            else:
                print("输入错误,重新输入")
        print("修改成功!")

    else:
        print("没有此学生,修改失败!")


def display():
    print("学生的信息如下:")
    print("学号\t姓名\t性别")
    for temp in students:
        print("%4s\t%4s\t%4s\t" % (temp['stuID'], temp['name'], temp['sex']))
    print()


def quit():
    print("即将退出系统")
    print()


def menu():
    print("****学生管理系统****")
    print("1.添加学生信息")
    print("2.查询学生信息")
    print("3.删除学生信息")
    print("4.修改学生信息")
    print("5.显示学生信息")
    print("6.退出系统")



def main():
    while True:
        menu()
        key = input("请输入选择操作的序号:")
        if key == '1':
            insertStu()
        elif key == '2':
            selectStu()
        elif key == '3':
            deleteStu()
        elif key == '4':
            editStu()
        elif key == '5':
            display()
        elif key == '6':
            quit()
            break
        else:
            print("操作错误!请重新输入")


main()



你可能感兴趣的:(用python编写学生管理系统 -v1)