Python interface to achieve a simple landing

Claim:

1. Enter the user name and password

2. After successful authentication display a welcome message

3. After unsuccessful attempts to enter the lock

Code is implemented as follows:

#  -*- coding: utf-8 -*-
"""
@Author:yhf1995
@time:2020/1/28 9:41
"""
from openpyxl import load_workbook
# 从openpyxl 导入 load_workbook

user_excel = load_workbook(r"user_password.xlsx")
# 导入整个excel工作簿,excel表格分为工作簿、sheet表单和单元格
user_list = user_excel.get_sheet_by_name('Sheet1')
# 导入user_password.xlsx表格中的第一个表单sheet1的数据,存放到user_excel
# 该表格有三列,第一列是用户名,第二列是密码,第三列是登录错误次数,其初始值为0
user_number = user_list.max_row
# 调用user_list.max_row方法获得表的最大行数,max_column为获取最大列数
#print(user_list.cell(row=6,column=3).value)
# 测试,打印第6行第3列的值
#print(user_list.cell(row=2,column=1).value)
#同上

count = 0
# 作为用户登录成功的标志位,登录成功后count值变为1,结束登录循环
while count != 1:
    username = input("username=")
    password = input("password=")
    # 输入用户名和密码
    for i in range(user_number):
        if username == user_list.cell(row=i+2,column=1).value and user_list.cell(row=i+2,column=3).value == 3:
            print("the user %s is locked" % (username))
            break
            # 本段是判断该用户是否登录错误超过3次,被lock了
        elif username == user_list.cell(row=i+2,column=1).value and password == user_list.cell(row=i+2,column=2).value:
            print("Welcome user %s login..." %(username))
            count = 1
            break
            # 判断登录成功,打印出“Welcome user XXX login...”
    else:
        print("Invalib name or password")
        for j in range(user_number):
            if username == user_list.cell(row=j + 2, column=1).value:
                user_list.cell(row=j + 2,column=3).value += 1
                user_excel.save('user_password.xlsx')
                break
                # 登录错误,登录错误的用户次数加1

Description: Python version is 3.6, to be the new front in the run .py folder named user_password.xlsx table, the table has three columns, the first is the user name, the second column is the line of the user's login password the third column is the number of errors in the log record, the initial value 0 of the third column.

In the case of a user name or password error, and the case where the user is locked, you can perform an unlimited number of users logged in, in the case of a successful login, only prints a welcome statement, the end of the run the program.

Note from the topic: Old Boys Python Education courses

 

Released three original articles · won praise 0 · Views 128

Guess you like

Origin blog.csdn.net/sinat_36852069/article/details/104100021