思维导图:
需求:输入用户名和密码,认证后显示欢迎信息,输错三次后锁定
#!/usr/bin/env python
#coding:utf-8import MySQLdbconn=MySQLdb.connect(user='root',passwd='123456',host='192.168.2.71',db='login')cur=conn.cursor()cur_user=conn.cursor()cur_user.execute("select * from t_account")cur.execute("select * from lock_account")user_names=cur_user.fetchall()lock_names=cur.fetchall()login_names={}#转换用户名和密码
for name,passwd in user_names: login_names[name]=login_names.get(name,passwd)print login_nameslock_users=[] #获取已经锁过的账号
for name in lock_names: names=name[1] lock_users.append(names)print lock_usersretry_counter = 0
while retry_counter < 3 :#循环三次 user = raw_input('Username:').strip() if len(user) ==0: #账号不为空 print "Username cannot be empty!" continue if user in lock_users: print "username is already locked" break passwd = raw_input('Password:').strip() if user in login_names and passwd==login_names[user]: print login_names[user] print "Welcome %s loging my system" % user break else: retry_counter+=1else: cur.execute("insert into lock_account(name) values(%s)",user) conn.commit() print lock_users print "Your are not allowed to login!,now will lock %s account" % user