设为首页 加入收藏

TOP

Python s12 Day1 笔记及作业(一)
2017-09-30 13:02:12 】 浏览:4706
Tags:Python s12 Day1 笔记 作业

作业一:

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

 

python3代码:

# -*- coding:utf-8 -*-
input_name = input("Please input your user name : ").strip()
user_lockfile = open("user_lockfile.txt","r+")
user_file = open("user_file.txt")
user_list = user_file.readlines()


for i in range(3):
    input_passwd = input("Please input your password : ").strip()
    #查找被锁用户列表判断是否被锁住
    if input_name in [locked_user.rstrip() for locked_user in user_lockfile.readlines()]:
        print("Sorry, your account is locked!")
        user_file.close()
        user_lockfile.close()
        exit(1)
    else: #没有被锁住,查找用户列表
        if input_name not in [user_record.split()[0] for user_record in user_list]:
            print("Sorry, your account doesn't exist!")
            user_file.close()
            user_lockfile.close()
            exit(2)
        else: #用户存在,判断密码是否正确,正确跳出循环
            input_record = input_name + ' ' + input_passwd
#            print(input_record)
#            print(user_file.tell()) #记录当前游标位置
            if input_record in [user_record.rstrip() for user_record in user_list]:
                print("Logging in...")
                user_file.close()
                user_lockfile.close()
                exit(0)
            else:#密码不正确,判断错误次数,错误三次(i=2)将用户锁住
                if i == 2:
                    user_lockfile.write(input_name + "\n")
                    print("Sorry, you're locked!")
                    user_file.close()
                    user_lockfile.close()
                    exit(3)
                else:#剩余机会数 2-i
                    chance = 2 - i
                    print("Wrong password! %s chances left!" % chance)

 

userfile:

wayne 123
root shroot123

user_lockfile:

kevin
james
root

 

 

 

作业二:

  • 三级菜单
  • 可以此选择进入各子菜单
  • 所需知识点:列表、字典

 

python3代码:

 

# -*- coding:utf-8 -*-
import time
import os

DICT_PRO = {"北京":["大兴区","朝阳区","海淀区","东城区","西城区","丰台区","通州区"],
            "上海":["静安区","徐汇区","浦东新区","虹口区","普陀区","长宁区","宝山区","嘉定区","闵行区"],
            "山东省":["烟台市","济南市","青岛市"],
            "辽宁省":["大连市","沈阳市"],
            "香港":None,
            "澳门":None,
            "台湾":["台北市"]}
DICT_CIT = {"烟台市":["莱山区","芝罘区","福山区","龙口市"],
            "青岛市":["李沧区","台东区","崂山区"],
            "济南市":["市中区","天桥区","历城区"],
            "大连市":["中山区","甘井子区","沙河口区","高新园区"],
            "沈阳市":["皇姑区","大东区","和平区","铁西区"]}

#将输入的数字转化成整数形式
def input_verify(choice):
    if str.isdigit(choice):
        choice = int(choice)
    return choice

#输出框架
def framework_show(province='', city='', district=''):
    os.system('cls')   #清屏
    print('''
######################################################
*                                                    *
*          欢迎进入省市区查询系统                    *
*                                                    *
######################################################
*                                                    *
*          省份 : %s  城市 : %s  区 : %s
*                                                    *
######################################################
    ''' % (province, city, district))

#展示欢迎界面
def welcome_show(province='', city='', district=''):
    print('''
######################################################
*                                                    *
*            Welcome to %s %s %s
*                                                    *
######################################################
    ''' % (province, city, distr
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python学习笔记(1)----python安.. 下一篇Python s12 Day1 笔记及作业

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目