基于Python的用户管理小系统,包含文件读写操作,实现了用户信息注册和登录的简单功能。
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
class userLogReg: """ Created on 2018.11 @author: tox33 """ def __init__( self ): """ :param userFile: 操作的文件 """ self .userFile = "user.txt" def userLogin( self ,username,password): """ 用户登录 :param username:用户名 :param paaword:密码 :return:True,用户登录成功;False,用户登录失败 """ try : f = open ( self .userFile, "r" ,encoding = "utf-8" ) for line in f: line = line.strip() line_list = line.split( "#" ) if line_list[ 0 ] = = username and line_list[ 1 ] = = password: return True elif line_list[ 0 ] = = username and line_list[ 1 ] ! = password: print ( "密码错误!!" ) return False except IOError: return False def userRegister( self ,username,password): """ 用户注册 1、打开文件 2、用户名#密码 :param username:用户名 :param password:密码 :return:True,注册成功 """ with open ( self .userFile, "a" ,encoding = "utf-8" )as f: temp = "\n" + username + "#" + password f.write(temp) return True def user_exist( self ,username): """ 检测用户名是否存在 :param username:要检测的用户名 :return: True,用户名存在;False,用户名不存在 """ try : with open ( self .userFile, "r" ,encoding = "utf-8" ) as f: for line in f: line = line.strip() line_new = line.split( "#" ) if line_new[ 0 ] = = username: return True return False except IOError: return False def main( self ): """ 主控制函数 :操作选择参数arg: 0-注册 1-登录 """ while ( True ): arg = input ( "0:注册 ,1:登录\n" ) if arg = = "0" : user = input ( "请设置用户名:" ) if self .user_exist(user): print ( "用户名已存在,请重新设置!" ) continue else : pwd = input ( "请设置密码:" ) if self .userRegister(user,pwd): print ( "注册成功!" ) continue else : print ( "注册失败!" ) continue elif arg = = "1" : user = input ( "请输入用户名:" ) if not self .user_exist(user): print ( "用户名不存在,请检查!" ) continue else : pwd = input ( "请输入登录密码:" ) if self .userLogin(user,pwd): print ( "登录成功!" ) break else : print ( "登录失败,请检查!" ) continue else : print ( "输入错误,请检查!" ) continue if __name__ = = '__main__' : test = userLogReg() test.main() |
参考网址:Python登录注册验证功能实现
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/tox33/article/details/83691011