标签:inpu 内容 sdi continue odi 退出 文件 工资 exit
‘‘‘
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")
标签:inpu 内容 sdi continue odi 退出 文件 工资 exit
原文地址:http://www.cnblogs.com/hqd2008/p/7643006.html