python dict学习:模拟用户登录行为

来源:互联网 发布:计算机与网络期刊等级 编辑:程序博客网 时间:2024/05/08 05:12
#!/usr/bin/env pythondb={}def newuser():    prompt="login desired:"    while True:        name=input(prompt)        if name in db:            prompt="name taken,try another:"            continue        else:            break    pwd=input("password input:")    db[name]=pwddef olduser():    name=input("login:")    pwd=input("passwd:")    passwd=db.get(name)    if name not in db:        prompt = "name incorrect"    if passwd==pwd:        print("Welcome back",name)    else:        print("passwd incorrect")def showmenu():    prompt="""    (N)ew User Login    (E)xisting User Login    (Q)uit    Enter choice:"""    done=False    while not done:        chosen=False        while not chosen:            try:                choice=input(prompt).strip()[0].lower()            except(EOFError,KeyboardInterrupt):                choice='q'            print('\nYou picked: [%s]' % choice)            if choice not in 'neq':                print('invalid option ,tray again')            else:                chosen=True    if choice=='q':        done=True    if choice=='n':        newuser()    if choice=='e':        olduser()if __name__=='__main__':    showmenu()    newuser()    olduser()
0 0
原创粉丝点击