Python实现⽤户名和密码登录
本⽂实例为⼤家分享了Python实现⽤户名和密码登录的具体代码,供⼤家参考,具体内容如下
功能
登录及注册,密码错误多次后验证码确认
说明
初次运⾏,程序将会⾃动⽣成⼀个名为user的⽂本⽂档,是包含⽤户名及密码的字典
输⼊⽤户名,如果⽤户名不存在,程序会⾃动以输⼊的⽤户名进⾏注册
输⼊密码,当输错4次时,程序会⽣成⼀个4位验证码,并使⽤vbs⽅式弹出,如果验证码输错,程序退出,否则重新执⾏主循环
代码
from os import system
from sys import exit
from random import randint
from time import sleep
user={'root':'88888888'}
error_time=4
mode=False
chack=[None,None]
user_name=''
user_passwd=[None,None]
#读取⽤户
try:
f=open('','r')
user=ad())
f.close()
except:
f=open('','w')
f.write("{'root':'88888888'}")
f.close
user={'root':'88888888'}
#main
while True:
user_name=str(input('请输⼊⽤户名>'))
#判断⽤户是否存在
if user_name not in user:#⽤户不存在 -> 注册 -> 设置⽤户名
print('⽤户不存在,将执⾏注册操作。')
if ' ' in user_name:
print('\aErr: ⽤户名中不能有空格')
elif user_name=='':
print('\aErr: ⽤户名不能为空')
else:
#设置密码
while True:
user_passwd[0]=str(input('请设置密码>'))
if ' ' in str(user_passwd[0]):
print('\aErr: 密码中不能含有空格。')
elif user_passwd[0]=='':
print('\aErr: 密码不能为空。')
elif len(user_passwd[0])<6:字符串函数注册登录
print('\aErr: 密码长度太短,⾄少6位。')
else:
#再次输⼊密码
user_passwd[1]=str(input('请再次输⼊密码>'))
if user_passwd[0]!=user_passwd[1]:
print('\aErr: 两次输⼊的密码不⼀致。')
else:
print('注册成功!\n\n请重新登录:')
user[user_name]=user_passwd[0]
#写⼊⽂件
f=open('','w')
f.write(str(user))
f.close()
break
else:    #⽤户存在 -> 登录 -> 确认密码是否正确
#错4次后验证码确认
while error_time!=0:
user_passwd[0]=input('请输⼊密码 4/'+str(error_time)+'>')
if user_passwd[0]!=user[user_name]:
print('\aErr: 密码错误')
error_time=error_time-1
else:
mode=True
break
else:
#验证码确认
print('\n\a\a因错误次数过多,进⾏验证码确认')
chack[0]=str(randint(999,10000))    #⽣成验证码
#写⼊到VBS⽂件,并弹出
f=open('chack.vbs','w')
f.write('msgbox("验证码>'+str(chack[0])+'<")')
f.close()
system('start chack.vbs')
#验证验证码
chack[1]=str(input('请输⼊验证码>'))
if chack[0]!=chack[1]:
print('\aErr: 验证码错误!')
#倒计时退出
for i in range(3,-1,-1):
print('\b'*23+'程序将在 '+str(i+1)+' 秒后退出...',end='',flush=True)
sleep(1)
exit(0)
else:
error_time=4
if mode==True:
break
input('登录成功...')
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。