文件内容 test.txt
leo
123
1
python操作(操作的是在文件写入用户名,密码和输错次数,如果输错次数会加1,且不能超多三次,三次就会自动执行锁定操作)
1 f = open("F:/python/test.txt") # 返回一个文件对象 2 name = f.readline() # 调用文件的 readline()方法 3 pwd = f.readline() 4 num=int(f.readline()) 5 f.close() 6 #----------------------判断输入账户与文件的是否相同 7 name = name[0:len(name)-1] 8 pwd = pwd[0:len(pwd)-1] 9 len_name=len(name) 10 while num<3: 11 guess_name=input("name:") 12 guess_pwd = input("pwd:") 13 #print(guess_name," ",len_name) 14 if guess_name==name and guess_pwd==pwd: 15 print("same") 16 break 17 else: 18 num+=1 19 print("not same") 20 else: 21 print("太多次,账户已锁定 ",num) 22 print(num) 23 f=open("F:/python/test.txt","w+") 24 li=[name,' ',pwd,' ',str(num)] 25 f.writelines(li) 26 f.close()