python实现用户账号密码登录且密码星号显示

 

       项目需要,需要在程序的入口做一个用户登录界面,密码需要星号显示。附上源程序和运行结果,环境是python3.7。

代码实例:

#输入账号和密码
def encryption():
    import dnn
    import time
    init_usrname= "admin"
    init_password= "admin"
    flag0=0
    flag1=0
    print('>>>User Login<<<')
    while True:
        usr=input('   enter username:    ')
        if usr==init_usrname:
            while flag0<3:
                password= B()
                print(password)
                if password==init_password:
                    print()
                    print("+++++++++++++++++++++++++")
                    print('     Success Login!')
                    print("+++++++++++++++++++++++++")
                    print()
                    time.sleep(1)
                    flag1=1
                    break
                else:
                    flag0 +=1
                    if flag0<=2:
                       print('Wrong Password,enter again!')
            if flag1==1:
                 break
            flag0=0
            print ('You have tried three times,login again!')
        else:
            print ('Wrong Username,enter again!')
'''
    Python3的字符串的编码语言用的是unicode编码,由于Python的字符串类型是str,在内存中以Unicode表示,一个字符对应若干字节,如果要在网络上传输,或保存在磁盘上就需要把str变成以字节为单位的bytes
python对bytes类型的数据用带b前缀的单引号或双引号表示:

                  'ABC'

                  b'ABC'

要注意区分'ABC'和b'ABC',前者是str,后者虽然内容显得和前者一样,但bytes的每个字符都只占用一个字节
'''

#密码星号打印
def B():
    import msvcrt, os
    print('   enter password:    ', end='', flush=True)
    li = []
    while 1:
        ch = msvcrt.getch()
        #回车
        if ch == b'\r':
            return b''.join(li).decode() #把list转换为字符串返回
            break
        #退格
        elif ch == b'\x08':
            if li:
                li.pop()
                msvcrt.putch(b'\b')
                msvcrt.putch(b' ')
                msvcrt.putch(b'\b')
        #Esc
        elif ch == b'\x1b':
            break
        else:
            li.append(ch)
            msvcrt.putch(b'*')
    return b''.join(li).decode()
    #os.system('pause')

#测试
if __init__ == "main":
    encryption()

运行结果:

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值