Python密码登录程序的思考--学与习
# 初学者的起步,对于开始的流程图结构还不太熟悉
# 思考: 1,write()与writelines()的区别,前者确定为字符串,后者为序列(列表,字典、元组等),自动为你迭代输入
# 2. 文件结构的构建,选择什么样的序列存储数据,如何使用有待加强
3.文件的读写操作,读写格式的设置 r ,a,w三种类别,分为只读,只追加、只覆盖写;然后延伸至rb,wb,ab读写二进制文件应用于视频与图片的读写;其次r+为可读写两种操作 w+为可读写两种操作(f覆盖) a+为追加读写两种操作,最后rb+,wb+,ab+
1 # import getpass 2 3 4 def read_lock(): 5 f = open('lockfile', encoding='utf-8') 6 user_lock_info = f.read().split() 7 f.close() 8 return user_lock_info 9 10 11 def save_lock(lockname): 12 s = open("lockfile", 'a', encoding='utf-8') 13 s.writelines(lockname) 14 s.close() 15 16 17 def read_log(): 18 user_log = {} 19 f = open('log', encoding='utf-8') 20 user_log_info = f.readlines() 21 f.close() 22 for i in user_log_info: 23 user = i.split(" ", 2) 24 user_log[user[0]] = user[1] 25 # print(user_log) 26 return user_log 27 28 29 count = 0 30 while count < 3: 31 username = input('username:') 32 # password = getpass.getpass("password:") 33 lock = read_lock() 34 user_log = read_log() 35 if username not in user_log: 36 print("---该用户不存在---") 37 continue 38 if username in lock: 39 print("---Your account is Locked---") 40 continue 41 password = input("password:") 42 if user_log[username] == password: 43 print("---welcome to log in---") 44 break 45 else: 46 count += 1 47 print("----Your account or password is wrong,please input again!!-----") 48 else: 49 save_lock(username) 50 print(">>>>>The times of your wrong input is more than %s times<<<<<<\n " 51 ">>>>>so your account is locked<<<<<<" % count)