您好,登錄后才能下訂單哦!
使用Python3怎么實現一個工資管理系統?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
工資管理系統要求:
1. 查詢員工工資
2. 修改員工工資
3. 增加新員工記錄
4. 退出
執行代碼:
#!/usr/bin/env python3 # Author:Robert # --*-- coding: utf-8 --*-- def file_test(): with open("info.txt",'r',encoding="utf-8") as f: file = list(f) return file def server(): for i in range(1): print("1.查詢員工工資\n2.修改員工工資\n3.增加新員工記錄\n4.退出") return i loginSucces = False while True: i = server() serial_number = input("請選擇你的服務>>:") if serial_number.isdigit(): serial_number = int(serial_number) file = file_test() if serial_number < 5 and serial_number >= 0: if serial_number == 1: name = input("請輸入要查詢的員工姓名:").strip() for i in file: file = i.strip().split() if name in file: break for i in file: if name in file: name_sum = file[1] print("\033[32;1m%s的工資是:%s\033[0m\n"%(name,name_sum)) break else: print("\033[31;1m沒有此用戶!\033[0m\n") elif serial_number == 2: name_old = input("請輸入要修改的員工姓:").strip() for ii in file: file = ii.strip().split() if name_old in file: print(file) sum_old = file[1] name_alter,sum_alter = input("請輸入修改的新員工姓名和工資,用空格分割:").strip().split() with open("info.txt","r",encoding="utf-8") as f: lines = f.readlines() with open("info.txt","w",encoding="utf-8") as f_a: for line in lines: if name_old in line: line = line.replace(name_old,name_alter) f_a.write(line) f_a.close() with open("info.txt", "r", encoding="utf-8") as f: lines = f.readlines() with open("info.txt", "w", encoding="utf-8") as f_b: for line in lines: if name_alter in line: line = line.replace(sum_old,sum_alter) f_b.write(line) f_b.close() print("修改成功") break if loginSucces == True: break else: print("\033[31;1m沒有此用戶!\033[0m\n") elif serial_number == 3: try: name_new, sum_new = input("請輸入要增加的員工姓名和工資,用空格分割:").strip().split() except ValueError: print("\033[31;1m輸入的參數不夠!\033[0m\n") else: if name_new.isalpha() and sum_new.isdigit(): for ii in file: file = ii.strip().split() if name_new not in file: with open("info.txt", "a+", encoding="utf-8") as f_c: f_c.write("\n%s %s"%(name_new,sum_new)) print("增加成功") break if loginSucces == True: break else: print("\033[31;1m參數類型不對!\033[0m\n") elif serial_number == 4: print("\033[31;1m謝謝使用,下次再見!\033[0m\n") exit() else: print("\033[31;1m請輸入正確的序號!\033[0m\n") else: print("\033[31;1m輸入的不是整數!\033[0m\n")
info.txt
name salary
Alex 100000
Rain 80000
Egon 50000
Yuan 30000
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。