zoukankan      html  css  js  c++  java
  • day1 python简介 python2与python3 变量 判断 循环 用户登陆作业

    一、python简介

      1、动态类型语言

        python的变量可以在运行中复制,不需要提前定义类型,比如name = ’zhangxiaodong',变量name就是字符串类型

      2、强类型定义语言

        python的变量类型一旦确定后,如果不通过强制转换那么它就永远是这个类型了

        例子:a=‘5’  此时a是字符串类型,如果现在 print(a+1)则会报错‘TypeError: must be str, not int’,需要通过int(a)进行转换

      3、python比较有用的解释器

        CPython:官方默认的解释器,用C编写,也是目前linux上默认的解释器

        PyPy:对python代码进行动态编译,可以显著提高python的运行速度,绝大部分Python代码都可以在PyPy下运行,但是PyPy和CPython有一些是不同的  

        http://pypy.readthedocs.org/en/latest/cpython_differences.html

    二、python2 与 python3区别

      1、print()

        python2 print ‘xxx'

        python3 print()

        python2.7兼容部分python3版本,比如可以使用print()函数

      2、input  raw_input

        python2中raw_input交互输入的时候如果是字符必须要带上引号,否则就认为是变量,会出现变量没有定义的错误

        input()函数输入的时候默认都是字符串,需要进行强制转换

      3、字符集

        python2默认ASCII,python3默认unicode,因此python3默认支持中文

    三、变量

      1、变量定义的规则:

      • 变量名只能是 字母、数字或下划线的任意组合
      • 变量名的第一个字符不能是数字
      • 一般使用英语单词作为变量名,多单词组合时用下划线分割,如info_of_user用户的的信息。
      • 以下关键字不能声明为变量名
        ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

       2、赋值

        name = 'zhangxiaodong' #str

        number = 5 #int

        user = name

        变量是直接指向内存中的一个地址,如user=name,实际是user直接指向’zhangxiaodong‘这个字符在内存中的地址。

    四、判断、循环

      1、if判断

        

    _user = 'zhangxiaodong'
    _password = 'adb123'
    
    user = input('user:')
    password = input('passowrd:')

    if user == _user and password == _password:
      print('welcome to login...')
    elif user == _user:
      print('password erro')
    else:
      print('user or password error')

      

      2、for循环

    _oldboy_age = 50
    
    for i in range(3):
        oldboy_age = int(input('age;'))
        if oldboy_age == _oldboy_age:
            print('yes, you got it!')
            break
        elif oldboy_age > _oldboy_age:
            print('think bigger....')
        else:
            print('think smaller...')
    else:
        print('you have tried to many times, fuck off')
    

      3、while循环

    _oldboy_age = 50
    count = 0
    while count < 3:
        oldboy_age = int(input('age;'))
        if oldboy_age == _oldboy_age:
            print('yes, you got it!')
            break
        elif oldboy_age > _oldboy_age:
            print('think bigger....')
        else:
            print('think smaller...')
        count += 1
        if count == 3:
            continue_confirm = input('do you want to keeping guess:')
            print(continue_confirm)
            if continue_confirm != 'n':
                count = 0
            else:
                exit(0)
    else:
        print('you have tried to many times, fuck off')
    

     

    作业:

    1、编写登陆接口

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

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Glen
    
    #将文件转换为字典
    def file_to_dict(file_path, split_char):
        dict_name = {}
        with open(file_path, 'r') as f:
            for line in f.readlines():
                _username, _password = line.strip('
    ').split(split_char)
                dict_name[_username] = _password
        return dict_name
    
    #写入lock文件
    def write_to_lock(username, password, file_path):
        with open(file_path, 'a') as f:
            write_str = '{username} {password}
    '.format(username=username, password=password)
            f.write(write_str)
    
    d_all_user = file_to_dict('all_user', ' ')
    d_lock_user = file_to_dict('lock_user', ' ')
    
    count = 0
    while count < 3:
        username = input('usernaem:')
        password = input('password:')
        for k in d_lock_user:
            if username == k:
                print('locked...')
                exit(0)
        for (k, v) in d_all_user.items():
            if username == k and password == v:
                print('login successful...')
                exit(0)
        print('username or password wrong...')
        count += 1
    else:
        print('more than three times, locked...')
        write_to_lock(username, password, 'lock_user')
    

      

    2、多级菜单

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

     

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Glen
    
    country = {'中国': ['四川', '浙江']}
    province = {'四川': ['巴中', '成都', '内江'], '浙江': ['宁波', '杭州']}
    city = {'巴中': ['巴州区', '通江县'],
            '成都': ['双流县', '锦江区'],
            '内江': ['隆昌县', '威远县'],
            '宁波': ['鄞州区', '象山县'],
            '杭州': ['临安市', '西湖区']}
    
    current_level = country
    current_area = '中国'
    
    
    # 定义打印列表函数
    def print_area(level, area):
            for i in level:
                if i == area:
                    for value in level[area]:
                        print(value)
    
    
    command = ''
    is_end = False
    while not is_end:
        print_area(current_level, current_area)
        command = input('please enter command:')
        if command == 'quit':
            is_end = True
        elif command == 'return':
            if current_level == city:
                current_level = province
                for (k, v) in current_level.items():
                    if current_area in v:
                        current_area = k
            elif current_level == province:
                current_level = country
                current_area = '中国'
            else:
                is_end = True
        elif command in current_level[current_area]:
            if current_level == country:
                current_area = command
                current_level = province
            elif current_level == province:
                current_area = command
                current_level = city
            else:
                print('level 3....')
        else:
            print('you enter command wrong...')
    

      

  • 相关阅读:
    帝国 标签模板 使用程序代码 去除html标记 并 截取字符串
    iis6 伪静态 iis配置方法 【图解】
    您来自的链接不存在 帝国CMS
    帝国cms Warning: Cannot modify header information headers already sent by...错误【解决方法】
    .fr域名注册 51元注册.fr域名
    帝国网站管理系统 恢复栏目目录 建立目录不成功!请检查目录权限 Godaddy Windows 主机
    星外虚拟主机管理平台 开通数据库 出现Microsoft OLE DB Provider for SQL Server 错误 '8004' 从字符串向 datetime 转换失败
    ASP.NET 自定义控件学习研究
    CSS层叠样式表之CSS解析机制的优先级
    ASP.NET程序员工作面试网络收藏夹
  • 原文地址:https://www.cnblogs.com/starcor/p/9445478.html
Copyright © 2011-2022 走看看