码迷,mamicode.com
首页 > 编程语言 > 详细

Python作业模拟登陆(第一周)

时间:2017-10-16 23:33:47      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:/usr   ogr   模拟登陆   思路   作业   读取   ppi   with open   closed   

模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户

思路:

1. 用户名密码文件为passwd,锁定用户文件为lock

2. 用户输入账号密码采用input输入,分割passwd文件出user,passwd字段并比较input的user和passwd

3.当用户三次输入错误后将input user写入到lock文件,读取时判断是否在lock文件中存在

 

代码展示(Python 3.6):

技术分享
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: Colin Yao
 4 """模拟登陆作业"""
 5 import time,sys,getpass
 6 print("Welcome Please input your username and shopping_db ")
 7 account_file = passwd
 8 lock_file = lock
 9 count = 0
10 
11 while count <3:
12     username = input("username : ")
13     password = input("Passwd : ")
14     #password = None
15     #password = getpass.getpass("Passwd : ") #可以使用密文或者明文
16     lock_f = open("lock", "r+")
17     lock_userlist = lock_f.readlines()
18 
19     for lock_user in lock_userlist:
20         lock_user = lock_user.strip(\n)
21         if username == lock_user:
22             print("%s is lockd 30s  later this program  will end " % lock_user)
23             time.sleep(30)
24             sys.exit()
25 
26     with open("passwd", "r") as f:
27         userlist = f.readlines()
28         for user_line in userlist:
29             (user, passwd) = user_line.strip(\n).split()
30             if username == user:
31                 if password == passwd:
32                     print(welcome %s  % username)
33                     sys.exit(0)
34                 elif password == None:
35                         print("not allow none")
36             else:
37                 pass
38         else:
39             print("sorry username or shopping_db wrong try again" )
40         count += 1
41 
42 else:
43     lock_f.write(username + \n)
44     sys.exit("please restart and user:%s is locked " %username)
45 lock_f.close()
View Code

passwd文件内容

colin 123456
python 123456
golang 123456

lock文件内容

colin1
colin2

  

Python作业模拟登陆(第一周)

标签:/usr   ogr   模拟登陆   思路   作业   读取   ppi   with open   closed   

原文地址:http://www.cnblogs.com/sean-yao/p/7679248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!