需求:
一.输入用户名密码,正确后登陆系统,打印
1.打印个人信息
2.修改个人信息
3.修改密码
二.每个选项写一个方法
三.登陆时输错3次退出登陆
# 请先运行3-6行代码,然后注释掉 # with open('accounts.txt', 'w', encoding='utf-8') as f: # f.write('alex,123,李python,24,Enginner,IT,132132 ') # f.write('rain,112233,雨,25,Teacher,Teaching,132132 ') # f.write('ann,123123,李安,27,customer service,service,132132 ') # 正式代码 USER_INFO = {} with open('accounts.txt', 'r', encoding='utf-8') as f2: for line in f2: user_list = line.strip().split(',') USER_INFO[user_list[0]] = { 'username': user_list[0], 'password': user_list[1], 'name': user_list[2], 'age': user_list[3], 'job': user_list[4], 'dept': user_list[5], 'phone': user_list[6] }def modify_user_password(username): count = 0 while True: count += 1 old_password = input('请输入旧密码:').strip() if old_password == USER_INFO[username]['password']: count2 = 0 while True: count2 += 1 new_password = input('设置新密码:') new_password2 = input('请在输入一遍:') if new_password2 == new_password: USER_INFO[username]['password'] = new_password2 with open('accounts.txt', 'w', encoding='utf-8') as f4: for user in USER_INFO: f4.write(user + ',' + USER_INFO[user]['password'] + ',' + USER_INFO[user]['name'] + ',' + USER_INFO[user]['age'] + ',' + USER_INFO[user]['job'] + ',' + USER_INFO[user][ 'dept'] + ',' + USER_INFO[user]['phone'] + ' ') exit('修改成功') else: if count2 == 3: print('输入三次失败,请重新输入') break print('两次密码输入不一致,请重新输入') else: if count == 3: print('登陆三次失败,自动退出') break print('密码错误') def modify_user_info(username): user_info_key = [] user_info_value = [] print(' -------------------- 修改个人信息 --------------------') for index, key in enumerate(USER_INFO[username]): if index < 2: pass else: user_info_key.append(key) user_info_value.append(USER_INFO[username][key]) while True: print('personal data', user_info_key) string_user_info = ''' 0.Name: {0} 1.Age: {1} 2.Job: {2} 3.Dept: {3} 4.Phone: {4} '''.format(USER_INFO[username]['name'], USER_INFO[username]['age'], USER_INFO[username]['job'], USER_INFO[username]['dept'], USER_INFO[username]['phone'] ) print(string_user_info) choice = input('请按编号选择,按q退出').strip() if choice.isdigit() and 0 <= int(choice) <= 5: choice = int(choice) print('current value %s' % user_info_value[choice]) new_value_key = user_info_key[choice] new_value = input('new value:').strip() user_info_key[choice] = new_value USER_INFO[username][new_value_key] = new_value print(USER_INFO[username]) with open('accounts.txt', 'w', encoding='utf-8') as f3: for user in USER_INFO: f3.write(user + ',' + USER_INFO[user]['password'] + ',' + USER_INFO[user]['name'] + ',' + USER_INFO[user]['age'] + ',' + USER_INFO[user]['job'] + ',' + USER_INFO[user]['dept'] + ',' + USER_INFO[user]['phone'] + ' ' ) print('修改成功') elif choice.lower() == 'q': exit('再见') else: print('输入不合法,请按编号重新输入') def verify_user(): count = 0 while True: count += 1 username = input('username:').strip() if username in USER_INFO: password = input('password:').strip() if password == USER_INFO[username]['password']: while True: choices_info = ''' -------------------- welcome {0} -------------------- 1.打印个人信息 2.修改个人信息 3.修改密码 '''.format(USER_INFO[username]['username']) person_info = ''' -------------------- Name: {0} Age: {1} Job: {2} Dept: {3} Phone: {4} -------------------- '''.format(USER_INFO[username]['name'], USER_INFO[username]['age'], USER_INFO[username]['job'], USER_INFO[username]['dept'], USER_INFO[username]['phone']) print(choices_info) choice = input('请选择,按q退出').strip() if choice.isdigit() and 0 <= int(choice) <= 3: choice = int(choice) if choice == 1: print(person_info) elif choice == 2: modify_user_info(username) else: modify_user_password(username) elif choice.lower() == 'q': exit('再见') else: print('请输入1~3之间的数字') else: if count == 3: print('登陆三次失败,系统自动退出') break else: print('用户名或密码错误,请重新输入') else: if count == 3: print('登陆三次失败,系统自动退出') break print('用户名不存在') verify_user()