python设计登录程序_python实现简单登陆流程

登陆流程图:

代码实现:

#-*- coding=utf-8 -*-

import os,sys,getpass

'''

user.txt 格式

账号 密码 是否锁定 错误次数

jack 123 unlock 0

tom 123 unlock 0

lily 123 unlock 0

hanmeimei 123 unlock 0

lucy 123 unlock 0

'''

# 定义写入文件的函数

def wirte_to_user_file(users,user_file_path):

user_file = file(user_file_path,'w+')

for k,v in users.items():

line = []

line.append(k)

line.extend(v)

user_file.write(' '.join(line)+'\n')

user_file.close()

# 判断用户文件是否存在,不存在直接退出

user_file_path = 'users.txt'

if os.path.exists(user_file_path):

user_file = file(user_file_path,'r')

else:

print 'user file is not exists'

sys.exit(1)

# 遍历用户文件,将用户包装成字典

users_dic = {}

for user_line in user_file:

user = user_line.strip().split()

users_dic[user[0]] = user[1:]

'''

{

'lucy': ['123', 'unlock', '0'],

'lily': ['123', 'unlock', '0'],

'jack': ['123', 'unlock', '0'],

'hanmeimei': ['123', 'unlock', '0'],

'tom': ['123', 'unlock', '0']

}

'''

while True:

# 输入账号

input_name = raw_input('please input your username,input "quit" or "q" will be exit : ').strip()

# 判断是否为退出

if input_name == 'quit' or input_name == 'q':

sys.exit(0)

# 输入密码

password = getpass.getpass('please input your password:').strip()

# 判断账号是否存在、是否锁定

if input_name not in users_dic:

print 'username or password is not right'

break

if users_dic[input_name][1] == 'lock':

print 'user has been locked'

break

# 判断密码是否正确,正确,登陆成功

if str(password) == users_dic[input_name][0]:

print 'login success,welcome to study system'

sys.exit(0)

else:

# 如果密码错误则修改密码错误次数

users_dic[input_name][2] = str(int(users_dic[input_name][2])+1)

# 密码错误次数大于3的时候则锁定,并修改状态

if int(users_dic[input_name][2]) >= 3:

print 'password input wrong has 3 times,user will be locked,please connect administrator'

users_dic[input_name][1] = 'lock'

wirte_to_user_file(users_dic,user_file_path)

break

wirte_to_user_file(users_dic,user_file_path)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值