#edit by weiwei xu
def printout():
with open('menu', 'r', encoding='utf-8') as ff:
print1= ff.read().split()
for index, n in enumerate(print1):
print(index + 1, n)
choice = input(">>:")
return choice
while True:
m = printout()#调用函数
if m.isdigit():
choice = int(m)
if 1 == choice:
name = input("Input the name ")
with open('info.txt', 'r', encoding='utf-8') as f:
mm = f.read().split()
for i in mm:
if i == name:
w = i.index(name) # 获取列的下标
print(name + '的工资是: %s' % mm[w + 1])
printout()
continue
else:
print("The name is wrong,try again:")
break
elif 2 == choice:
with open("info.txt", "r+", encoding="utf-8") as f:
with open("info1.txt", "w+", encoding='utf-8') as f_w:
input1 = input("input your name and salary with space connect")
input4 = input1.split() # 将字符串转换为列表
name1 = str(input4[0])#取用户名,确定输入名称是否正确。
salary1 = input4[1]
for line in f: # 这里的line值是类似"Alex 100000\n",带换行的,如下的line.line.strip去掉换行,f_w里是加上换行,如果没有此操作,会导致插入的那行和后面一行数据连接在一起
line = line.strip()
if name1 in line:
line = line.replace(line, input1)#替换用户名为name1的那行值
f_w.write(line + '\n')
print("修改成功")
with open('info1.txt','r',encoding='utf-8') as f_w:#重新读取info1的文件写入到info
read1=f_w.read()
with open('info.txt', 'w+', encoding='utf-8') as f:
f.write(read1)
#f.flush()
elif 3==choice:
input2=input("请输入要增加的员工姓名和工资,用空格连接")
input3= input2.split() # 将字符串转换为列表
name2= str(input3[0]) # 取用户名,确定输入名称是否正确。
salary2 = input3[1]
with open('info.txt','a+',encoding='utf-8') as ff3:
ff3.write(input2+'\n')
ff3.flush()
elif 4==choice:
print("再见")
exit()
else:
print("The number is no exit")
break
else:
print("Please input the int:")
break
#####
#######
因为之前已经写过很多判断字符串和int型数据,所以此作业里就不在进行判断。
转载于:https://www.cnblogs.com/xuxiaole/p/8699042.html