3-16函数练习题

修改个人信息程序:

在一个文件里存多个人的个人信息,如下:

username password age position department

alex         abc123     24    Engineer  IT

rain         df2@423   25    Teacher  Teching

……

1、输入用户名密码,正确后登录系统,打印

1.修改个人信息

2.打印个人信息

3.修改密码

2、每个选项写一个方法

3、登录时输错3次退出程序

def change_info(pinfolist):
    s = '1.年龄\n2.职务\n3.部门'
    while True:
        print(s)
        choice = int(input('choose please:'))
        if choice <= 3 and choice >=1 :
            new_info = input('new information:')
            pinfolist[choice + 1] = new_info
            save(pinfolist)
        else:
            break           
    
def print_info(pinfolist):
   for value in pinfolist:
       print(value,end=',')
   print('')
    
def change_pwd(pinfolist):
    new_pwd = input('new password:')
    pinfolist[1] = new_pwd
    save(pinfolist)


def save(pinfolist):
    file_data = ''
    new_str = ','.join(pinfolist)
    with open('人员信息.txt','r') as f:
        for line in f:
            if pinfolist[0] in line:
                line = new_str
            file_data += line
    with open('人员信息.txt','w') as f:
        f.write(file_data)

def user_register(name,pwd):
    f = open('人员信息.txt','r')
    while True:
        text_line = f.readline()
        if text_line:
           if text_line.find(name) != -1 and text_line.find(pwd) != -1:
              infolist = text_line.split(',')
              f.close()
              return infolist     
        else:
            break
    f.close()
    return None
    

count = 0   
while True:
    while count < 3:
        username_in = input("用户名:")
        userpwd_in = input("密码:")
        personal_info_list = user_register(username_in,userpwd_in)
        if  personal_info_list == None:
            count += 1
            print('name or password error,input again.....')
            continue
        else:
            print('登录成功.........')
            s_choice = '1.修改个人信息\n2.打印个人信息\n3.修改密码\n4.退出登录'
            while True:              
                print(s_choice)
                user_choice = int(input('choose you want:'))
                if user_choice == 1:
                    change_info(personal_info_list)
                elif user_choice == 2:
                    print_info(personal_info_list)
                elif user_choice == 3:
                    change_pwd(personal_info_list)
                else:
                    break
            break       
    if count == 3 :      
        print('已输入三次错误,退出登录.....')
    break

猜你喜欢

转载自www.cnblogs.com/echo-kid-coding/p/11287700.html