python从入门到精通(DAY 3)
要求:编写登陆接口
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定
针对此实例写了有二种类型的脚本,略有不同,具体如下:
帐号文件account.txt内容如下:
sam 123
david 12
kevin 123
lin 12
tailen 123
jack 12
锁文件account_lock.txt默认为空
一、只针对帐号文件里的用户进行判断并锁定,针对用户和密码各有三次错误重试机会。
1、流程图如下:
代码如下:
#!/usr/bin/python27 #_*_ coding:utf-8 _*_ import sys,os,getpass os.system('clear') i = 0 while i < 3: #只要用户登录异常不超过3次就不断循环 name = raw_input("请输入用户名:") lock_file = open('account_lock.txt','r+') #当用户输入用户名后,打开LOCK 文件 以检查是否此用户已经LOCK了 lock_list = lock_file.readlines() for lock_line in lock_list: #循环LOCK文件 lock_line = lock_line.strip('\n') #去掉换行符 if name == lock_line: #如果LOCK了就直接退出 sys.exit('用户 %s 已经被锁定,退出' % name) user_file = open('account.txt','r') #打开帐号文件 user_list = user_file.readlines() for user_line in user_list: #对帐号文件进行遍历 (user,password) = user_line.strip('\n').split() #分别获取帐号和密码信息 if name == user: #如用户名正常匹配 j = 0 while j < 3: #只要用户密码异常不超过3次就不断循环 passwd = getpass.getpass('请输入密码:') #输入隐藏密码 if passwd == password: #密码正确,提示欢迎登录 print('欢迎登录管理平台,用户%s' % name) sys.exit(0) #正常退出 else: print('用户 %s 密码错误,请重新输入,还有 %d 次机会' % (name,2 - j)) j += 1 #密码输入错误后,循环值增加1 else: lock_file.write(name + '\n') #密码输入三次错误后,将该用户追加到LOCK文件 sys.exit('用户 %s 达到最大登录次数,将被锁定并退出' % name) else: pass #当用户没匹配时,跳过并继续循环 else: print('用户 %s 不存在,请重新输入,还有 %d 次机会' % (name,2 - i)) i += 1 #当用户输入错误时,循环值增加1 else: sys.exit('用户 %s 不存在,退出' % name) #用户输入三次错误后,异常退出 lock_file.close() #关闭LOCK文件 user_file.close() #关闭帐号文件
二、针对帐号文件里的不存在的用户也可以进行判断并锁定,针对用户和密码共有三次错误重试机会
代码如下:
#_*_ coding:utf-8 _*_ import sys,os,getpass os.system('clear') retry_limit = 3 retry_count = 0 account_file = 'account.txt' lock_file = 'account_lock.txt' while retry_count < retry_limit: #只要重试不超过3次就不断循环 username = raw_input('\033[31;43mUsername:\033[0m') username = username.strip() lock_check = open(lock_file) #当用户输入用户名后,打开LOCK 文件 以检查是否此用户已经LOCK了 for line in lock_check.readlines(): #循环LOCK文件 if username == line.strip('\n'): #去掉换行符 sys.exit('\033[35mUser %s is locked!!!\033[0m' % username) #如果LOCK了就直接退出 password = raw_input('\033[32;41mPassword:\033[0m') #输入密码 f = open(account_file,'r') #打开帐号文件 match_flag = False # 默认为Flase,如果用户match 上了,就设置为 True for line in f.readlines(): user,passwd = line.strip('\n').split() #去掉每行多余的\n并把这一行按空格分成两列,分别赋值为user,passwd两个变量 if username == user and password == passwd: #判断用户名和密码是否都相等 print('hello, %s !!' % username) match_flag = True #相等就把循环外的match_flag变量改为了True break #然后就不用继续循环了,直接 跳出,因为已经match上了 f.close() if match_flag == False: #如果match_flag还为False,代表上面的循环中跟本就没有match上用户名和密码,所以需要继续循环 print('sorry,%s is unmatched' % username) retry_count += 1 #计数器加1 else: print('wlecome login my learning system!') break #用户成功登录,退出脚本 else: print("you account %s is locked!!!" % username) g = open(lock_file,'a') g.write(username) #被锁用户追加到用户锁文件 g.write('\n') g.close()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.
