变量、基本数据类型-练习👈

一、第一大题

1、病毒程序需要定期将监控到的数据写入日志文件,请记录下日志文件路径C:\a\b\c\adhsvc.dll.system32,方便后期处理

log_file = r'C:\a\b\c\adhsvc.dll.system32'

2、病毒程序在上传文件时,发送的报头数据里需要包含文件信息:文件名a.txt、大小360,请记录下文件信息

header_data = {
    'filename': 'a.txt',
    'size': 360,
}

3、程序运行过程中有一段错误日志需要记录下来,错误日志为"上传文件失败"

error_log = '上传文件失败'

4、假设我收到一条信息要记录,信息为中病毒客户端的信息"[2020-02-18-17:00:48] 癞蛤蟆病毒感染者-> 80.82.70.187:33649 正在上传数据"

important_records = """
[2020-02-18-17:00:48] 癞蛤蟆病毒感染者-> 80.82.70.187:33649 正在上传数据
"""

5、把服务端ip地址存放下来,ip地址为10.0.10.11

ip_address = '10.0.10.11'

6、病毒程序需要每隔3秒才运行一次,请记录下这个时间间隔

time_interval = 3

二、第二大题:嵌套取值操作

1、第1小题:争对列表

students_info = [['egon', 18, ['play', ]], ['alex', 18, ['play', 'sleep']]]

# 需求:请取出第一个学生的第一个爱好
print(students_info[0][-1][-1])

2、第2小题:争对字典

info = {
    'name': 'egon',
    'hobbies': ['play', 'sleep'],
    'company_info': {
        'name': 'Oldboy',
        'type': 'education',
        'emp_num': 40,
    }
}
# 需求:请取出取公司名
print(info['company_info']['name'])

3、第3小题:针对列表和字典

students = [
    {'name': 'alex', 'age': 38, 'hobbies': ['play', 'sleep']},
    {'name': 'egon', 'age': 18, 'hobbies': ['read', 'sleep']},
    {'name': 'wupeiqi', 'age': 58, 'hobbies': ['music', 'read', 'sleep']},
]
# 需求:取第二个学生的第二个爱好
print(students[1]['hobbies'][-1])

三、选做题

选做题:编写用户登录接口

  • 1、输入账号密码完成验证,验证通过后输出"登录成功"
  • 2、可以登录不同的用户
  • 3、同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
  • 扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分
# 用户名、密码、对应用于的错误次数都是不同属性的类型,存放在字典中。为了控制用程序退出,用户的个人信息不会丢失以及用户错误的次数不会丢失,把一下数据存入文件中。
account_info = """
{'username': 'egon', 'password': '123', 'wrong_number': 0}
{'username': 'alex', 'password': '123', 'wrong_number': 0}
{'username': 'wangmao', 'password': '123', 'wrong_number': 0}
"""

# 判断存放用户账户信息的文件是否存在,存在把把文件内容读出,不存在把用户账户文件写入
account_file = r'accounts.txt'

if not os.path.isfile(account_file):
    with open(account_file, 'w') as f:
        f.write(account_info)
# 定义一个记录用户登录成功以及锁定解锁,不需要继续没必要的判断
are_you_ok = False

# 定义一个用户登录的当前状态,用户登录成功以后不在判断
login_list = []

while True:
    inp_username = input("请输入账号>>:").strip()
    inp_password = input("请输入密码>>:").strip()

    # 判断用户是否以及登录了,登了就别登了啊!
    if inp_username in login_list:
        print('哦!我亲爱的【%s】,你已经登录了,不要在登陆了哦!' % inp_username)
        continue

    # 文件修改操作
    with open(account_file, 'r') as f_read, open('swap.txt', 'w') as f_write:
        for line in f_read:
            if not len(line.split()):
                continue

            # 判断are_you_ok
            if are_you_ok:
                f_write.write(line)
                continue

            current_account = eval(line.strip(r'\n'))
            username = current_account['username']
            if username != inp_username:
                # 用户名不存在,写入交换文件,继续检索
                f_write.write(line)
                continue

            wrong_number = current_account['wrong_number']
            # 判断用户的错误次数是否达到三次
            if wrong_number == 3:
                print(f'抱歉[{username}],之前错误次数达到[3次],占时锁定3秒')
                import time

                time.sleep(3)  # 这里用3秒替代
                # 解锁以后,重置用户的错误记录
                current_account['wrong_number'] = 0
                f_write.write(f'{current_account}\n')
                are_you_ok = True
                continue

            password = current_account['password']
            if inp_password == password:
                print('恭喜登录,成功!')
                # 登录成功,重置用户的错误记录,记录当前用户登录状态
                current_account['wrong_number'] = 0
                f_write.write(f'{current_account}\n')
                are_you_ok = True
                login_list.append(username)
            else:
                # 登录失败,记录用户错误次数
                current_account['wrong_number'] += 1
                print(f'啊![{username}]你已经错了[{current_account["wrong_number"]}次]了啊!')
                f_write.write(f'{current_account}\n')
        else:
            are_you_ok = False
    os.remove(account_file)
    os.rename(r'swap.txt', account_file)

posted @ 2020-03-04 22:00  给你加马桶唱疏通  阅读(309)  评论(0编辑  收藏  举报