在一个文件里存多个人的个人信息,如以下
username password age position department
alex abc123 24 Engineer IT
rain df2@432 25 Teacher Teching
....
1.输入用户名密码,正确后登录系统 ,打印
1. 修改个人信息
2. 打印个人信息
3. 修改密码
2.每个选项写一个方法
3.登录时输错3次退出程序
1 #!/usr/bin/env python 2 # -*- coding=utf-8 -*- 3 """ 4 @author:Wllen 5 @file:staff_manage.py 6 @time:2018/6/8 14:57 7 """ 8 def print_info(account_dict,username): # 打印用户信息,将输入的username作为字典的key进行取值(列表),打印列表 9 data = account_dict.get(username) 10 info = ''' 11 姓名: %s 12 年龄: %s 13 职务: %s 14 部门: %s 15 ''' % (data[0], data[2], data[3], data[4]) 16 print(info) 17 # save_account(account_dict) 18 19 def save_account(account_dict): # 将修改后的数据存入文件中 20 with open(account_file, "r+", encoding="utf-8") as f: 21 f.truncate(0) # 清空原文件,光标位置向后切片 22 for j in account_dict: # 将字典中的key的值存入文件中 23 row = ",".join(account_dict[j]) 24 f.write("%s \n" % row) 25 f.flush() 26 27 def change_info(account_dict,username): # 修改用户个人信息 28 data = account_dict.get(username) 29 # print(data) 30 info = ''' 31 1 姓名 32 2 年龄 33 3 职务 34 4 部门 35 ''' 36 while True: # 一直停留在这 37 print(info) 38 change_choice = input("请选择需要修改的选项:").strip() # 输入选择的功能 39 if change_choice.isdigit(): 40 change_choice = int(change_choice) 41 if change_choice > 0 and change_choice <= 4: # 输入的不能超过功能数 42 if change_choice == 1: 43 data_name = input("请输入新的用户名:").strip() 44 if not data_name: 45 print("输入有误!") 46 else: 47 data[0] = data_name 48 print("已将用户名修改为:", data_name) 49 elif change_choice == 2: 50 data_age = input("请输入新的年龄:").strip() 51 if not data_age: 52 print("输入有误!") 53 else: 54 data[0] = data_age 55 print("已将年龄名修改为:", data_age) 56 elif change_choice == 3: 57 data_position = input("请输入新的职务:").strip() 58 if not data_position: 59 print("输入有误!") 60 else: 61 data[0] = data_position 62 print("已将职务修改为:", data_position) 63 elif change_choice == 4: 64 data_department = input("请输入新的部门:").strip() 65 if not data_department: 66 print("输入有误!") 67 else: 68 data[0] = data_department 69 print("已将部门修改为:", data_department) 70 else: 71 print("没有此选项") 72 elif change_choice.lower() == "b": 73 break 74 else: 75 print("输入有误!") 76 save_account(account_dict) 77 def change_passwd(account_dict,username): 78 data = account_dict.get(username) 79 # print(data) 80 while True: 81 new_passwd = input("请输入您的新密码:").strip() 82 new_passwd1 = input("请再次输入您的密码:").strip() 83 if not new_passwd: 84 print("您的输入有误!") 85 else: 86 if new_passwd == new_passwd1: 87 data[1] = str(new_passwd) 88 print("密码修改成功!") 89 break 90 else: 91 print("两次密码不相同,请重新输入!") 92 save_account(account_dict) 93 94 account_file = "staff_db.db" 95 with open(account_file, "r+", encoding="utf-8") as f: 96 raw_data = f.readlines() 97 accounts = {} 98 # 把账户数据从文件里读书来,变成dict,key为用户名 99 for line in raw_data: 100 line = line.strip() 101 items = line.split(",") 102 accounts[items[0]] = items 103 # {'alex': ['alex', 'abc123', '24', 'Engineer', 'IT'], 'rain': ['rain', 'df2@432', '25', 'Teacher', 'Teching']} 104 105 menu = ''' 106 1. 修改个人信息 107 2. 打印个人信息 108 3. 修改密码 109 ''' 110 count = 0 111 while count < 3: 112 username = input("请输入您的用户名:").strip() 113 password = input("请输入您的密码:").strip() 114 if username in accounts: 115 if username == accounts.get(username)[0] and password == accounts.get(username)[1]: 116 print("欢迎%s来到员工信息表登记系统!" % username) 117 while True: 118 print(menu) 119 choice = input("请输入您需要进行的操作:").strip() 120 if choice.isdigit(): 121 choice = int(choice) 122 if choice == 1: 123 change_info(accounts, username) 124 print(accounts.get(username)) 125 elif choice == 2: 126 print_info(accounts, username) 127 elif choice == 3: 128 change_passwd(accounts, username) 129 else: 130 print("没有此选项,请重新输入!") 131 elif choice.lower() == 'q': 132 exit("系统正在退出......") 133 else: 134 print("输入有误,请重新输入!") 135 else: 136 print("对不起,您的用户名或密码错误,请重新再试!剩余登录次数%s" % (2-count)) 137 count += 1 138 else: 139 print("没有此用户!")