首页 > 代码库 > Python 基础 - Day 1 Assignment - Login 模拟登陆

Python 基础 - Day 1 Assignment - Login 模拟登陆

作业要求:

1. 用户输入帐号密码进行登陆

2. 用户信息保存在文件内

3. 用户密码输入错误三次后锁定用户

SAMPLE 1: By 刘

步骤:

1. 字符串读取转成字典

user_info = Eric:123456#Catherine:098765
user_list = user_info.split(#)
print(user_list)

user_dic ={}      # 创建空字典
for item in user_list:
    item_list = item.split(:)  # >>> [‘eric‘,‘123456‘,‘‘Catherine‘,‘098765‘]
    user_dic[item_list[0]] = item_list[-1]   #???
print(user_dic)

 {‘Eric‘: ‘123456‘, ‘Catherine‘: ‘098765‘}

2. 从文件中读取字符串

f = open(user_info.txt,r)
user_info = f.read()
f.close()

user_list = user_info.split(#)
print(user_list)

user_dic ={}      # 创建空字典
for item in user_list:
    item_list = item.split(:)  # >>> [‘eric‘,‘123456‘,‘‘Catherine‘,‘098765‘]
    user_dic[item_list[0]] = item_list[-1]   #
print(user_dic)

3. 程序的主要部分

import sys

user_info = {alex: "123",wenwei: gdalex}

count = 0
username = input(用户名 >>> : )
if username in user_info:
    # lock_list = 读取锁定文件的信息
    lock_list = [alex]
    if user_info in lock_list:
        print(被锁定)
        sys.exit()
    else:
        while count < 3:
            password = input(pwd >>>:)
            if password == user_info[username]:
                print(登陆成功,欢迎%s % username)
                sys.exit()
            else:
                count += 1
                if count == 3:
                    print(被说点,请联系管理员)
                    sys.exit()
                else:
                    print(密码错误!请重新输入。还有%s次机会 % (3-count))
else:
    print(用户名不存在)

 

sample 2: By 

Python 基础 - Day 1 Assignment - Login 模拟登陆