标签:strip ice [] item 验证 readlines 多个 login 通过
内容:
1.修改个人信息程序
2.员工信息增删改查
一、修改个人信息程序
需求:
1 在一个文件中存入多个人的个人信息,如以下: 2 username password age position department 3 alex abc123 21 Engineer IT 4 rain xyz456 32 Teacher Teaching 5 6 功能: 7 1.输入用户名及密码,正确登陆系统后显示 8 (1)修改个人信息 9 (2)打印个人信息 10 (3)修改密码 11 2.上述每个选项写一个方法 12 3.登陆输错3次退出程序
实现:
1 # 员工信息文件 -> info_employee.txt 2 3 # 员工信息(列表嵌套) -> [ "alex", "abc123", 21, "Engineer", "IT"] 4 info = [] 5 6 7 # 获取员工信息 8 def get_info(): 9 with open("info_employee.txt", ‘r‘, encoding="utf-8") as f: 10 # 读取每一行的数据存入列表中 11 details = f.readlines() 12 # 遍历列表将每一行的数据分开并将一行的数据作为一个列表存入info列表中 13 for detail in details: 14 detail = detail.strip().split() 15 info.append(detail) 16 17 18 # 登陆 19 def login(): 20 users_pwds = [] 21 # 获取用户名及密码: 22 for index in range(1, len(info)): 23 # 获取用户数据 24 detail = info[index] 25 # 获取用户名及密码 26 user = detail[0] 27 pwd = detail[1] 28 users_pwds.append(dict({user: pwd})) 29 # 输入用户名及密码并验证用户名及密码是否正确 30 for i in range(4): 31 if i == 3: 32 print("你已经输错3次,不能再登陆了!") 33 exit() 34 # 输入用户名及密码 35 36 username, password = input("input the username and password(eg: user pwd): ").strip().split() 37 for item in users_pwds: 38 if item.get(username, None) == password: 39 print("登陆成功!欢迎%s!" % username) 40 now_user = username 41 return now_user 42 else: 43 print("用户名或密码错误!请重新输入") 44 45 46 # 打印选项 47 def output_chose(): 48 # 打印选项 49 print("你有如下选项: ") 50 print("1.修改个人信息") 51 print("2.打印个人信息") 52 print("3.修改密码") 53 print("4.退出系统") 54 # 用户输入选项,函数返回选项对应的整数 55 while True: 56 choice = input("请输入选项(数字): ") 57 if choice.isdigit(): 58 choice = int(choice) 59 if 1 <= choice <= 4: 60 return choice 61 else: 62 print("请输入正确的数字选项!") 63 else: 64 print("请输入正确的数字选项!") 65 continue 66 67 68 # 将修改后的信息写入文件中 69 def write_info(): 70 with open("info_employee.txt", "w", encoding="utf-8") as f: 71 for item in info: 72 for value in item: 73 f.write(str(value)+"\t") 74 f.write("\n") 75 76 77 # 1.修改个人信息 78 def change_info(update_name): 79 index = 0 80 for personal in info: 81 if personal[0] == update_name: 82 for i in range(len(personal)): 83 print(i+1, info[0][i], ": ", personal[i]) 84 choice = int(input("请输入你想要修改的选项: ")) 85 print("修改前的值: ", info[index][choice-1]) 86 change = input("请输入修改后的值: ") 87 info[index][choice-1] = change 88 write_info() 89 index += 1 90 91 92 # 2.打印个人信息 93 def output_info(personal_name): 94 for personal in info: 95 if personal[0] == personal_name: 96 for i in range(len(personal)): 97 print(info[0][i], ": ", personal[i]) 98 99 100 # 3.修改密码 101 def change_pwd(change_name): 102 index = 0 103 # 遍历找到用户信息的位置 104 for item in info: 105 if item[0] == change_name: 106 break 107 index += 1 108 # 输入两次密码,一样就通过,否则就重新输入 109 while True: 110 pwd1 = input("请输入新密码: ") 111 pwd2 = input("请再一次输入新密码: ") 112 if pwd1 == pwd2: 113 info[index][1] = pwd1 114 print("密码输入正确,修改密码成功!") 115 write_info() 116 break 117 else: 118 print("两次输入的密码不一样请重新输入!") 119 120 121 if __name__ == ‘__main__‘: 122 get_info() 123 login_user = login() 124 while True: 125 number = output_chose() 126 if number == 1: 127 change_info(login_user) 128 if number == 2: 129 output_info(login_user) 130 if number == 3: 131 change_pwd(login_user) 132 if number == 4: 133 exit()
二、员工信息增删改查
需求:
实现:
标签:strip ice [] item 验证 readlines 多个 login 通过
原文地址:https://www.cnblogs.com/wyb666/p/8891340.html