Python---学生卡片

# 需要完成的基本功能:
# 1.	添加名片
# 2.	删除名片
# 3.	修改名片
# 4.	查询名片
# 5.	退出系统
# 程序运行后,除非选择退出系统,否则重复执行功能
id_list = []
import shelve
flag = 0  #判断是否存在某id卡片
s = shelve.open("student", "c",writeback=True)
# f = open("stunamelist.txt","ra+")
def find(id):
    '''
    查找是否存在该卡片
    :param id: 学生id
    :return: 
    '''
    global flag
    flag = 0
    global s
    try:
        s1 = s[id]
    except:
        flag = 0
        print("flag is 0")
    else:
        flag = 1
        print("flag is 1")

def add():
    '''
    实现增加学生卡片功能
    :return: void
    '''
    # global arr
    global s
    id = input("please enter the id")
    find(id)
    if flag==0:
        name = input("please enter the name")
        age = input("please enter the age")
        gender = input("please enter the gender")
        arradd={"id":id,"name":name,"age":age,"gender":gender}
        print("added!")
        s[id] = arradd
        s.sync()
        id_list.append(id)
        # f.write(id)
        # f.write("\n")
        print(s[id])
    else:
        print("sorry,this card already exists")

def delete(id):
    '''
    实现删除学生卡片功能
    :param num: id
    :return: void
    '''
    global s
    find(id)
    if flag==1:
        del s[id]
        s.sync()
        print("deleted!")
        i = 0
        for item in id_list:
            if item==id:
                del id_list[i]
                break
            i += 1
    else:
        print("sorry,this card does not exists")

def change(id):
    '''
    实现改变学生卡片功能
    :param id: id
    :return: void
    '''
    global s
    find(id)
    if flag == 0:
        print("sorry,can't find the card")
    else:
        while True:
            con=input("1-修改名字,2-修改年龄,3-修改性别,0-退出修改")
            dic = s[id]
            if con=='1':
                namec=input("please enter the name of the card you want to change")
                dic["name"] = namec
            elif con=='2':
                agec = input("please enter the age of the card you want to change")
                dic["age"] = agec
            elif con=='3':
                genderc = input("please enter the gender of the card you want to change")
                dic["gender"] = genderc
            elif con=='0':
                s[id] = dic
                s.sync()
                break
            else:
                print("illegal input")

def query(id):
    '''
    实现查询学生卡片功能
    :param num: id
    :return: 学生信息
    '''
    i = find(id)
    if flag==0:
        print("sorry,can't find the card")
    else:
        print(s[id])

def all():
    '''
    实现查询所有学生卡片功能
    :return:
    '''
    global id_list
    if len(id_list)>0:
        i = 0
        while i

你可能感兴趣的:(作业)