作业一:博客
作业二:编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
作业三:多级菜单
- 三级菜单
- 可依次选择进入各子菜单
- 所需新知识点:列表、字典
作业一分析:
readme.md
博客地址:http://www.cnblogs.com/wanghui1991
执行方式:Linux平台下安装好python3.x以上版本
chmod +x login.py
./login.py
数据:account.db村烦的事锁定的账户信息
login.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:wanghui
import os,sys,getpass #定义变量
lock_file = 'lock.db' #锁定用户信息
account_file = 'account.db' #正确用户信息
counter = 0 #初始化计数器
while counter < 3:
username = input('please input your name:').strip()
lock_check = open(lock_file,'r+')
account_check = open(account_file,'r+')
for u_line in lock_check.readline():
u_line = u_line.strip('\n')
if username == u_line:
sys.exit("%s is locked!!" %username)
password = getpass.getpass('please input your password:')
for p_line in account_check.readlines():
user,passwd = p_line.strip('\n').split()
if username == user and password == passwd:
sys.exit('user %s ,welcome to login!!'%username)
elif username != user:
sys.exit('username incorrect!!')
elif username == user and password != passwd:
print('retry again!!')
counter += 1
account_check.close()
else:
print('user %s is locked!!'%username)
lock_now = open(lock_file,'a')
lock_now.write(username)
lock_now.write('\n')
lock_now.close()