练习题:员工信息修改程序 在一个文件里存多个人的个人信息

# 练习题
# 员工信息修改程序
# 在一个文件里存多个人的个人信息,如以下
# username,password,age,position,department,phone
# alex,abc123,30,Engineer,IT,13651830433
# rain,df2@432,25,Teacher,Teching,18912334223
# 黑姑娘,df2@432,26,行政,人事,13811177306
# 需求:
# 1.输入用户名密码,正确后登录系统 ,打印
# 1. 修改个人信息
# 2. 打印个人信息
# 3. 修改密码
# 2.每个选项写一个方法
# 3. 当用户选择1时,提示用户选择要修改的字段,根据用户输入对相应字段进行修改
# 4.登录时输错3次退出程序
# 执行时应该达到的效果参考:
import re
# 1. 打印个人信息
def print_pessonal_info(dic_mess,username):
    list_mess = dic_mess.get(username)
    name = list_mess[0]
    age=list_mess[2]
    job=list_mess[3]
    dept=list_mess[4]
    phone=list_mess[5]
    info='''
    - - - - - - - -Info - - - - - - - - - 
    Name : %s
    Age  : %s
    Job  : %s
    Dept : %s
    Phone: %s
    - - - - - - - - - - - - - - - - - - - 
    '''%(name,age,job,dept,phone)
    print(info)
# 2. 修改个人信息
def chenge_pessonal_info(dic_mess,username):
    list_change = dic_mess.get(username)
    print("Pessonal info : %s"%list_change)
    name = list_change[0]
    password=list_change[1]
    age=list_change[2]
    job=list_change[3]
    dept=list_change[4]
    phone=list_change[5]
    info='''
    - - - - - - - -Info - - - - - - - - - 
0.    Name    :  %s
1.    Password:  %s
2.    Age     :  %s
3.    Job     :  %s
4.    Dept    :  %s
5.    Phone   :  %s
    - - - - - - - - - - - - - - - - - - - 
    '''%(name,password,age,job,dept,phone)
    print(info)
    chenge_num = int(input("请输入你需要修改项的序号:"))
    print("你选择修改的项原内容是:%s " %list_change[chenge_num])
    if chenge_num > 0 and chenge_num < len(list_change)-1:
        chenge_new = input("请输入新的内容:")
        list_change[chenge_num] = chenge_new
        print(list_change)
    elif chenge_num == 0:
        print("用户名不可能修。")
    else:
        print("你输入的序号不正确。")
# 3. 保存修改后文件
def save_back_to_file(dic_mess):
    f_message = open(file="message",mode="w",encoding="utf-8")
    for save_list in dic_mess:
        str_dic=str(dic_mess[save_list])
        str_dic=str_dic.lstrip("[")
        str_dic=str_dic.rstrip("]")
        str_dic=re.sub("'","",str_dic)
        str_dic = re.sub(" ", "", str_dic)
        f_message.write(str_dic+"\n")
        print(str_dic)
    f_message.close()

#读写文件内容,定义一个空字典
f_message = open(file="message",mode = "r+",encoding="utf-8")
mess = f_message.readlines()
dic_mess={
     }

for line in mess:
    line=line.strip()
    if not line.startswith("#"):
        items=line.split(",")  #拆分成列表
        dic_mess[items[0]]=items  #变成字典

menu = '''
1.打印个人信息
2.修改个人信息
3.修改密码
'''

count = 0
logo_suss = 0
while count < 3:
    username = input("请输入用户名:")
    password = input("请输入密码:")
    if username in dic_mess:
        key=dic_mess.get(username)
        if key[1] == password:
            print("- - - - - - welcome %s - - - - - -"%username)
            logo_suss = 1
            break
        else:
            print("你输入的用户名和密码不正确,请重新输入。")
            count += 1
    else:
        print("你输入的用户名和密码不正确,请重新输入!")
        count += 1
else:
    print("你输入的错误次数太多了")

if logo_suss ==1:
    print(menu)
    num = int(input("请输入你要操作的序号:"))
    if num ==1:
        print_pessonal_info(dic_mess,username)
    elif num ==2:
        chenge_pessonal_info(dic_mess,username)
        save_back_to_file(dic_mess)



你可能感兴趣的:(练习题:员工信息修改程序 在一个文件里存多个人的个人信息)