网络编程
位置:首页>> 网络编程>> Python编程>> Python实现注册、登录小程序功能

Python实现注册、登录小程序功能

作者:几何分布  发布时间:2022-12-03 17:50:11 

标签:python,登录,小程序,注册

主要实现功能

1、用户输入用户名,在用户名文件中查找对应的用户,若无对应用户名则打印输入错误

2、用户名输入正确后,进行密码匹配。输入密码正确则登录成功,否则重新输入。
3、连续输错三次密码则该用户名被锁,退出程序

--------------------------------------------------

在程序文件夹下建立一个用户名、密码的文件 :user_np.txt和一个用于存放被锁用户名的文件:lock.txt

--------------------------------------------------


#setencoding=utf-8

import os,sys
#将用户名文件内容读取到内存中
user = open('user_np.txt')
account_list = user.readlines()
user.closeloginSucess = 0       #控制while循环退出

lock_n = False#判断用户输入的用户名是否被锁的标志位

while True:

username = input('please input username:').strip()    #strip()函数是忽略空格
if len(username) == 0:
print('输入用户名不能为空')
continue
else :
print('输入用户名不为空')
l = open('lock.txt')
l_list = l.readlines()
l.close()
print(l_list)
for j in l_list:
j = j.strip('\n')
if username == j:
print('该用户已经锁定,请输入其他用户名')
lock_n = True
del j          #删除变量j
break
else:
continue
if lock_n is True:
lock_n = False
continue
else:
lock_n = False
for i in account_list:
i = i.split()             #split()函数是对括号中的符号进行切割
if username == i[0]:
for x in range(3):
password = input('please input password:').strip()
if password == i[1]:
loginSucess = 2
break
else:
print('The password is error')
#匹配正确或者遇到break程序就跳出循环体下面语句不执行
else:       #输入超过三次,将用户名写入锁文件并打印出来
print('%s ,input password is beyond three times,going to lock'%username)
l = open('lock.txt','a')
l.write(username+'\n')      #将要锁的用户名写入锁文件并且换行
l.close()
view = open('lock.txt')      #打开锁文件
print(view.read())          #打印锁文件的内容,方便自己做调试
loginSucess = 1
if loginSucess ==2:                
print('sucess info')
break
elif loginSucess ==1:
print('用户名被锁,请重新输入')
else:
print('输入错误')

下面通过代码看下python实现注册登录小程序

用python 实现模拟注册和登录的程序:用户信息最终以字典的格式储存在一个txt文件里,具体实现如下:

users.txt里用户字典格式如下:


{
  'name': {'password': '111111', 'role': '1'},
  'name2': {'password': '222222', 'role': '1'},  
  'name3': {'password': '222222', 'role': '1'}
}

# 注册
f = open('users.txt', 'a+', encoding='utf-8')
f.seek(0)
user_info = eval(f.read())# 字符串转字典
i =0
while i<3:
  i += 1
  uname = input("请输入用户名:").strip()
  upass = input("请输入密码:").strip()
  passC = input("请确认密码:").strip()
  if not uname or not upass or not passC:
    print("注册失败,用户名或密码不能为空")
    continue
  if upass != passC:
    print("注册失败,两次输入密码不一致")
    continue
  if uname in user_info:
    print("注册失败,用户名已存在")
    continue
  print("恭喜你,注册成功!")
  user_info[uname] ={'password':upass,'role':'1'}
  f.seek(0)
  f.truncate()
  f.write(str(user_info))
else:
  print("sorry,register over 3 times bye-bye!")
f.close()
# 登录
fr = open('users.txt', 'r')
fr.seek(0)
user_info = eval(fr.read())
j = 0
while j<3:
  j +=1
  uname = input("请输入用户名:\n").strip()
  upass = input("请输入密码:\n").strip()
  if not uname:
    print("用户名不能为空")
    continue
  if not upass:
    print("密码不能为空")
    continue
  if uname not in user_info:
    print("用户名不存在")
    continue
  if upass != user_info[uname]['password']:
    print("密码错误")
    continue
  print("恭喜你,登录成功!")
else:
  print("sorry! login over 3 times bye-bye! ")
f.close()

运行结果:

Python实现注册、登录小程序功能

以上所述是小编给大家介绍的Python实现登录、注册小程序功能网站的支持!

来源:https://www.cnblogs.com/xwxiao/archive/2018/09/21/9686467.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com