服务器之家

服务器之家 > 正文

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

时间:2021-04-04 00:05     来源/作者:几何分布

主要实现功能

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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里用户字典格式如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
  '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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部