工資系統--練習檔案的操作(增改查),工資--

來源:互聯網
上載者:User

工資系統--練習檔案的操作(增改查),工資--

'''
1、使用的是字典儲存
2、沒有進行異常捕獲
3、必須建立info檔案,檔案內容格式為:
{'name': 'xiaoli', 'salary': 9990}
{'name': 'xiaowang', 'salary': 8000}
'''
def memu():
print("1. 查詢員工工資")
print("2. 修改員工工資")
print("3. 增加新員工記錄")
print("4. 退出")

def find_wages(_name):
with open("info","r",encoding="utf-8") as f:
flag = 0
for line in f:
if line.strip() == "":
continue
line = eval(line)
if line["name"] == _name:
print("\033[31;1m %s的工資是%d \033[0m"%(line["name"],line["salary"]) + '\n')
flag = 1
if flag == 0:
print("\033[36;1m 員工中沒有%s \033[0m"%_name)

def modify_wages(_name):
list = []
with open("info","r",encoding="utf-8") as f:
for line in f:
flag = 0
if line.strip() == "":
continue
line = eval(line)
if line["name"] == _name:
salary = input("請輸入修改後的工資 >>>")
if salary.isdigit():
salary = int(salary)
else:
print("\033[36;1m 輸入錯誤 \033[0m")
line["salary"] = salary
print("\033[32;1m %s修改後的工資是%d \033[0m"%(line["name"],line["salary"]) + '\n')
else:
if flag == 0:
print("\033[36;1m 員工中沒有%s \033[0m"%_name)
flag = 1
list.append(line)
with open("info","w",encoding="utf-8") as f1:
for temp in list:
f1.write(str(temp) + '\n')

def add_wages():
dict = {}
name = input("請輸入你要新增的員工姓名 >>>")
salary = input("請輸入新增員工的工資 >>>")
if salary.isdigit():
salary = int(salary)
else:
print("\033[36;1m 輸入錯誤 \033[0m")
dict["name"] = name
dict["salary"] = salary
print("\033[35;1m 新增員工%s的工資是%d \033[0m" % (dict["name"], dict["salary"]) + '\n')
with open("info","a",encoding="utf-8") as f:
f.write(str(dict) + '\n')

while True:
memu()
choose = input("請輸入你要進行的操作 >>>")
if choose.isdigit():
choose = int(choose)
else:
print("\033[36;1m 輸入錯誤 \033[0m")
continue
if choose == 1:
name = input("請輸入要查詢的員工姓名 >>>")
find_wages(name)
elif choose == 2:
name = input("請輸入要修改的員工姓名 >>>")
modify_wages(name)
elif choose == 3:
add_wages()
elif choose == 4:
exit()
else:
print("\033[36;1m 輸入錯誤 \033[0m")

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.