Python实现用户管理系统
用户管理系统:
实现功能:
- 增添删除用户
- 用户登录
- 显示用户信息
直接上代码:
def Init_Gui(): #初始界面(给提供用户选择)
print '*' * 35
str = """ Welcome To User Login System: *
1.Resgiter: *
2.Login: *
3.Del User: *
4.Show: *
5.Choice: *
6.Exit: *"""
print str
print '*' * 35
user_info = { #字典保存用户信息
"name": [],
"passwd": [],
"age": [],
"gender":[]
}
def create_user(): #增加用户函数
new_user = raw_input("Input Username:>>>")
if new_user in user_info["name"]: #判断用户是否存在
print "User Exists"
create_user()
else:
user_info["name"].append(new_user) #添加用户名到字典中的用户名列表
passwd = raw_input("Input passwd:>>>")
user_info["passwd"].append(passwd) #添加密码
# age = raw_input("Input age>>>")
# user_info["age"].append(age) #
gender_choice = (0,1,2)
while True:
gender = raw_input("Input gender(1:Male,0:Female,2:Else):>>>")
if not gender:
user_info["gender"].append("None")
break
elif gender.isdigit():
if int(gender) in gender_choice:
user_info["gender"].append(gender) #添加性别信息
break
else:
print "Incorrect Number:"
else:
print "Incorrect gender"
while True:
age = raw_input("Input Age:>>>") #添加年龄信息
if not age:
user_info["age"].append("None")
break
elif age.isdigit():
user_info["age"].append(age)
break
else:
print "Illeagal age,please try again:"
print "Successfuly Registered!!!"
def del_user(): #删除用户函数
new_user = raw_input("Input Del_username:>>>")
if new_user in user_info["name"]: #若用户存在于列表
index = user_info["name"].index(new_user)
user_info["name"].remove(new_user)
del user_info["passwd"][index]
del user_info["age"][index]
del user_info["gender"][index] #删除所有用户信息
print "Successfuly deleted"
else:
print "The User doesn't exist" #否则输出用户不存在
def show_all(): #显示所有用户信息
if user_info["name"]: #判断列表是否为空
for index in range(len(user_info["name"])):
print "name:%s " % user_info["name"][index],
print "passwd:%s " % user_info["passwd"][index],
print "age:%s " % user_info["age"][index],
print "gender:%s " % user_info["gender"][index]
else:
print "The Database is NULL"
def log_in(): #用户登录函数
print "--------- User Login Interface-------"
username = raw_input("Useranme:>>>")
if username in user_info["name"]: #判断时有存在用户
i = 0
while i <3: #用户有三次输入密码机会
passwd = raw_input("Input Passwd:>>>")
index = user_info["name"].index(username) ##用户在用户列表的索引作为密码列表的索引
user_passwd = user_info["passwd"][index]
if passwd == user_passwd: #若密码匹配
print "Sussfully Logged in" #输入成功则跳出循环
break
else:
print "Incorrect Passwd:(You have 3 chances)"
print "This is %dst time " % (i+1)
i+=1
else:
print "Incorrect Username,please try again."
Init_Gui()
def choice():
while True:
input_choice = raw_input("Input Your Choice: >>>")
if input_choice.isdigit():
choice = int(input_choice)
else:
choice = 0
if choice == 1:
create_user()
elif choice == 2:
log_in()
elif choice == 3:
del_user()
elif choice == 4:
show_all()
elif choice == 5:
Init_Gui()
elif choice == 6:
exit()
else:
print "Reinput Choice:"
choice()
- 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
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
***********************************
Welcome To User Login System: *
1.Resgiter: *
2.Login: *
3.Del User: *
4.Show: *
5.Choice: *
6.Exit: *
***********************************
Input Your Choice:
>>>1 ##添加用户测试
Input Username:
>>>Vincent
Input passwd:
>>>redhat
Input gender(1:Male,0:Female,2:Else):
>>>1
Input Age:
>>>17
Successfuly Registered!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Input Your Choice:
>>>4 ##显示用户测试
name:Vincent passwd:redhat age:17 gender:1
- 1
- 2
- 3
Input Your Choice:
>>>2 ##用户登录测试
-------- User Login Interface-------
Useranme:
>>>Vincent
Input Passwd:
>>>redhat
Sussfully Logged in
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Input Del_username: #删除用户测试
>>>Vincent
Successfuly deleted
Input Your Choice:
>>>4
The Database is NULL