1. 程式人生 > >員工信息增刪改查

員工信息增刪改查

退出程序 alt username line 多個 打印 http date tails

內容:

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()

二、員工信息增刪改查

需求:

技術分享圖片

技術分享圖片

實現:

員工信息增刪改查