需求:

控制台根据用户选择实现:

     1、添加用户(个别信息必填)add user

     2、展示用户列表 list user

     3、根据名字搜索用户信息 search user

     4、根据名字修改用户信息 modify user

     5、退出 exit

 def add_user():
    filename=raw_input("你想保存到的文件名称为:")
    fo=open(filename,"a")
    print "请输入保存信息内容:"
    
    choise="y"
    while choise=="y":
        userdict={}
        userlist=[]
        name=raw_input("name:")
        age=raw_input("age:")
        sex=raw_input("sex:")
        phone=raw_input("phone:")
        userlist.append(age)
        userlist.append(sex)
        userlist.append(phone)
        userdict[name]=userlist
        content=str(userdict)
        fo.write(content+'\n')        
        choise=raw_input("是否继续?y/n:")
    fo.close()
    print "保存成功"
    return filename

def list_user():
    filename=raw_input("你想查看的文件名称为:")
    print "以下为所有学员信息:"
    fo=open(filename,"r")
    content=fo.readlines() 
    for dic in content:
        userdict=eval(dic)
        name=userdict.keys()[0]
        age=userdict.values()[0][0]
        sex=userdict.values()[0][1]
        phone=userdict.values()[0][2]
        print "-"*20
        print "name:", name
        print "age:",age
        print "sex:",sex
        print "phone:",phone   
    fo.close()


def search_user():
    filename=raw_input("你想搜索的文件名称为:")
    fo=open(filename,"r")
    content=fo.readlines()
   
    search_name=raw_input("你想查询信息的名字为:")
    for dic in content:
        userdict=eval(dic)
        if search_name in userdict.keys()[0]:
            age=userdict.values()[0][0]
            sex=userdict.values()[0][1]
            phone=userdict.values()[0][2]
            print "-"*20
            print "age:",age
            print "sex:",sex
            print "phone:",phone
    fo.close()

def modify_user():
    filename=raw_input("你想修改的文件名称为:")
    fo=open(filename,"r")
    content=fo.readlines() 
    modify_name=raw_input("你想修改信息的名字为:")
    for index, dic in enumerate(content):
        userdict=eval(dic)
        if userdict.keys()[0]==modify_name:
            choise=int(raw_input("你想修改的信息为(0为年龄,1为性别,2为电话):"))
            newcontent=raw_input("你想修改为:")
            userdict.values()[0][choise]=newcontent
            print "修改成功!"
            content[index]=userdict  
            break
    contentdict=content
    fo=open(filename,"w")        
    for i in contentdict:
        i=str(i)
        fo.write(i+'\n')
    fo.close()


def choice():
  
    print"\n"
    print "做一个选择:1,增加用户,2,用户列表,3,搜索用户,4,修改用户,5,退出!"
    print"\n"
    
    choice=raw_input("请做选择:")
    if choice=="1":
        add_user()
    elif choice == "2":
        list_user()
    elif choice=="3":
        search_user()
    elif choice=="4":
        modify_user()
    elif choice=="5":
        print'DONE!'
        exit()
    else:
        print "请输入1-5之间的整数:"     
choice()

 

posted on 2017-05-21 18:57  竹子的梦想在路上  阅读(1332)  评论(0编辑  收藏  举报