不是爬虫 ,这里主要是练习 下考虑问题的思路。
用户输入错误密码 3 次,将锁住,提示无法登陆。
第一步: 我将用户名和密码存在了F盘的EXCEL里,事实上肯定不会存这里的咯。只是练习下读取python读取excel表格的操作。
思考:用户名与密码是成对出现的 ,如果转换成dict格式的话,就非常容易办了。
# encoding=utf-8
import xlrd
import csv
# 输入用户名与密码 知识点;put 与 raw_input的区别
username= raw_input("plaese input your username:")
password = raw_input("please input your password:")
password = int(password)
# 读取F盘下的 excel表格 知识点:文件的 操作 # 注意反义字符“\\”
file_path= "E:\\test_user\\user_name.xlsx"
sheetname ="Sheet1"
dataresult = []
data_xlsl = xlrd.open_workbook(file_path)
table = data_xlsl.sheet_by_name(sheetname)
for i in range (0,table.nrows):
dataresult.append(table.row_values(i))
# 将list转换成dict 重点
dict_result = []
for i in range(1,len(dataresult)):
temp = dict(zip(dataresult[0],dataresult[i])) # dict的zip方法
dict_result.append(temp)
接下来就是逻辑判断了,难点就是,控制并记录用户输错密码的次数,大于3次就不给输了!!!
if password==news.values()[0] and username==news.values()[1]:#values()获取字典元素
print "login in successful"
if username !=news.values()[1] and password == news.values()[0]:
count = 0 # 计数
if username ==news.values()[1] and password != news.values()[0]:
print ("your password is not correctly")
for i in range (0,2):
count=count+1
password2 =raw_input("please input your password again:")
password2=int(password2) # 这个需要强制转换成int型,我也心累~~~~
if password2 == news.values()[0]:
print ("welcome to login")
break
else:
print ("password error")
if count == 2:
print ("password error more than 3 times")
总结:相当简陋的代码,测试时bug太多。只是记录下思维方向以及 for循环的练习。
还有,对于excel表格的操作,并将数据转换成dict的方法,我觉得灰常有用的奥。
无意间发现了一位大神写的,代码的思维比较严谨,直观。