Author: Zeno
date:2024/4/10
time:13:00
判断输入值是否在指定范围内
def isin(input, list):
if input in list:
return True, f'{input}'
else:
return False, f'{input} not found!'
输入用户名和密码,并进行判断
def getnp(func):
name_ipt = input(f'please input your {func} name:')
count = 0
while count < 3:
count += 1
pass_ipt = input(f'please input your {func} password:')
if pass_ipt.isalnum():
return name_ipt, pass_ipt
elif count == 3:
return False, 'Wrong password format input'
else:
print(f'''the password just allow alpha and number,
you have {3 - count} chance left, please try again:''')
continue
读取文件数据,转为字典
def read(file):
type = file.split('.')[-1]
data_dict = {}
if type in ['txt', 'xls']:
with open(f'{file}', "r", encoding='utf8') as f:
data = f.readlines()
data_list = [line.strip().split() for line in data if len(line) > 4]
if data_list:
for i in data_list:
name, pwd, role, state = i
data_dict[name] = {'name': name, "password": pwd, 'role': role, 'state': state}
return True, data_dict,data
else:
return False, 'no information now',''
else:
with open(f'{file}', 'rb') as f:
data = f.read()
tag,msg = read('data.txt')
print(tag,msg)
def write(file, dct, mode):
data_list = [list(i.values()) for i in list(dct.values())]
data_str = '\n'.join('\t'.join(i) for i in data_list)
with open(f'{file}', f'{mode}', encoding='utf8') as f:
f.write(data_str + '\n')
def logad(name):
if name:
tg, dct,dat = read('data.txt')
if dct[name]['role'] and dct[name]['role'] == 'admin':
return True, name
else:
return False, f'user {name} is not admin ,have no right!'
write('data1.txt',dct,'a')
verification_code = 'qaz'
def register(func='register'):
tag, dct,dat = read('data.txt')
dcts = {}
count = 0
while count < 3:
count += 1
name, pwd = getnp(func)
tag, msg = isin(name, dct)
if tag:
print(f'user {name} is existed,please try again')
continue
else:
rolevery = input('please input your role verification code:')
if rolevery == verification_code:
role = 'admin'
else:
role = 'visit'
state = 'open'
print(f'user {name} {func} successfully!')
dcts[name] = {'name': name, 'password': pwd, 'role': role, 'state': state}
write('data.txt', dcts, 'a')
break
def login(func=None):
name, pwd = getnp('login')
tag, dct,dat = read('data.txt')
isn,nm = isin(name,dct)
if tag and isn :
if pwd == dct[name]['password']:
login_now = []
login_now.append(name)
print(f' user {name} login successfully!')
if not func:
return True, name
else:
func(name=name)
else:
print('wrong login password')
return False, None
else:
print(nm)
def increase(name=None):
if login_now:
tag, namelg = logad(login_now[0])
if tag:
register('increase')
else:
print(namelg)
elif name:
tagn, namen = logad(name)
if tagn:
register('increase')
else:
print(namen)
else:
print(f'please login first!')
login(increase)
def pick(a,b):
if a:
return a[0]
elif b:
return b
elif a and b:
return b
def looks(name):
tag, namelg = logad(name)
if tag:
namef = input('please input the name look for a for all and name for the exact one:')
st, dct, dat = read('data.txt')
s,n = isin(namef, dct)
if namef == 'a':
dat_str = ''.join(dat)
print(f'''------------all -------------
{dat_str}
----------------------------------''')
elif s:
print(f'''----------------------------
information for {n} as below:
{list(dct[namef].values())}
''')
else:
print(namelg)
def look_for(name=None):
if login_now or name:
naml = pick(login_now,name)
looks(naml)
else:
print(f'please login first!')
login(look_for)
def delete(name=None):
...
verification_code = 'qaz'
login_now = []
def main():
count = 0
while count < 5:
print('''----------------Welcome to employee system--------------
1:register
2:login
3:increase
4:look_for
5:delete
----------------------------------------------------------''')
menu_dict = {'1': register, '2': login, '3': increase, '4': look_for, '5': delete}
menu_choice = input('please input your function choice in a above menu:')
tag, msg = isin(menu_choice, menu_dict)
if tag:
print(f'Welcome to {menu_dict.get(menu_choice).name}')
menu_dict.get(menu_choice)()
else:
print(f'{msg}')
main()
标签:4.0,管理系统,员工,print,input,login,else,data,name From: https://www.cnblogs.com/zenopan101861/p/18127329