要求
用户输入用户名密码
登陆成功显示欢迎信息
输错三次后锁定
流程图
代码
#coding:UTF-8 username_password_file = ‘user_passwd.txt‘ #存储用户名密码 f = file(username_password_file) username_password = {} for line in f.readlines(): line = line.split() username_password[line[0]] = line[1] #使用字典比使用两个数组更清晰 f.close() while True: lock_file = ‘lock.txt‘ #存储被锁定用户 f = file(lock_file) lock = [] for line in f.readlines(): line = line.strip() lock.append(line) f.close() user_name = raw_input(‘hello,welcome to here,please input your name:‘).strip() if user_name in username_password.keys(): if user_name in lock: print ‘you are locked‘ else: count = 0 #输入密码的计数器 while True: iden = 0 #作跳出循环的标志位 password = raw_input(‘Please input your password:‘).strip() if count >= 3: f = file(lock_file,‘a‘) f.write(user_name) f.write(‘\n‘) #加换行符 f.close() print ‘you input three times wrong,you are locked‘ break else: for k,v in username_password.items(): if user_name == k and password == v: print ‘hello,welcome to login in‘ iden = 1 break else: count = count + 1 print ‘The password is wrong,please input again‘ if iden == 1: break else: print ‘you input a wrong username,please input again‘
时间: 2024-11-05 12:11:32