用户和登录的实现原理和过程

#1.统计小写英文字符出现的次数
def countchrnum(s):
result={}
for c in s:
if c < 'a' or c > 'z':# 判断字母c < a 或者 c >z
continue
if c in result:
result[c] += 1
else:
result[c] = 1
return result
s='python test case'
print(countchrnum(s))

#实现用户登录和注册
#1.提示用户登录和注册
#2.注册流程 注意提示和字典处理
#3.登录过程 注意用户密码判断
#4.实现过注意函数定义和返回值
#5.代码优化 使用函数和字典的组合
usrinfo={}
def loginfunc():
#登录函数
uname = input('input user name:')
pwd = input('input user pwd:')
if uname not in usrinfo:
print(f'user name {uname} not exist')
elif pwd != usrinfo[uname]:
print('passwd is error')
else:
print('login success')

def refunc():
#注册函数
uname = input('input user name:')
pwd = input('input user pwd:')
if uname in usrinfo:
print('f{uname} exist,input again')
return
usrinfo[uname] = pwd
print('user list',usrinfo)

def select_menu():
tip = 'input [r] reg,\ninput [l] login:\n'
while True:
cmd = input(tip)
if cmd == 'r':
print('start reg')
refunc()
elif cmd == 'l':
print('start login')
loginfunc()
elif cmd == 'q':
break
else:
print('input error!')

def select_menu():
tip = 'input [r] reg,\ninput [l] login:\ninput [q] exit:\n'
opt_dict = {'r':refunc,'l':loginfunc}
while True:
cmd = input(tip)
if cmd == 'q':
break
func= opt_dict.get(cmd)
if func: #if func的值不为None 执行func函数
func()


if __name__ == '__main__':
select_menu()
 
posted @ 2022-05-22 19:11  人生信条~~  阅读(152)  评论(0编辑  收藏  举报