• python实训day2


    今天是python实训的第二天,在第一天的基础上,老师教了一些基础简单的操作,回去之后我也自己大量练习了,并做了作业

    1.作业:

    实现一个登陆程序

    代码如下

    with open(r'file1.text','w',encoding='utf-8') as f:
     3     f.write('用户名:lzh,密码:108595.
    用户名:lzh,密码:1998.')
     4 
     5 def login():
     6     user = ''
     7     pwd = ''
     8     dict1 = {}
     9     with open('file1.text','rt',encoding='utf-8')as w:
    10         for line in w:
    11             line = line.split('
    ')[0].split(',')
    12             for data in line:
    13                 if '用户名'in data:
    14                     user = data[4:]
    15                 else:
    16                     pwd = data[3:]
    17                 dict1[user] = pwd
    18     while True:
    19         user1 = input('请输入用户名:').strip()
    20 
    21         if user1 in dict1 :
    22             i = 1
    23             while i <= 3:
    24                 pwd1 = input('请输入密码:').strip()
    25                 if pwd1 == dict1[user1]:
    26                     print('登陆成功!')
    27                     break
    28                 else:
    29                     i = i + 1
    30             else:
    31                 print('密码错误超过三次!')
    32         else:
    33             print('用户不存在!')
    34         break
    35 
    36 login()
    运行结果
    请输入用户名:lzh
    请输入密码:123
    请输入密码:111
    请输入密码:000
    密码错误超过三次!
    

    2.学习内容

    今天上课讲了很多东西,主要笔记如下

    (1)

    字典是无序的
    1. 按照key取/存值
    >>>dict = {'name' : 'hy', 'age' : 23, 'sex' : 'male', 'school' : 'ahpu'}
    >>>print(dict['school'])         #取学校
    ahpu
    2. get()
    >>>print(dict.get('school'))
    ahpu
    >>>print(dict.get('sal'))
    None
    >>>print(dict.get('sal','15000'))     #第一个参数是key,第二个参数是默认值,若key存在则取对应值,否则取默认值
    15000
    3. len()
    >>>print(len(dict))
    4
    4. 成员运算in 和not in
    >>>print('name' in dict)
    True
    >>>print('sal' in dict)
    False
    5. 删除del
    >>>del dict['name']
    6. pop()
    >>>dict1 = dict.pop('name')
    >>>print(dict)
    {'age' : 23, 'sex' : 'male', 'school' : 'ahpu'}
    >>>print(dict1)
    hy
    >>>dict.popitem()     #随机pop一个
    >>>print(dict)
    {'name' : 'hy','age' : 23,'sex' : 'male'}
    7. keys(),values(),items()
    >>>print(dict.keys())
    dict_keys(['name', 'age', 'sex', 'school'])
    >>>print(dict.values())
    dict_values([ 'hy', 23, 'male', 'ahpu'])
    >>>print(dict.items())
    dict_items([('name', 'hy'), ('age', 23), ('sex', 'male'), ('school', 'ahpu')])
    8. 循环for
    #循环字典中所有的key
    >>>for i in dict:
           print(i)
    9. update()
    >>>dict2 = {'work' : 'student'}
    >>>dict.update(dict2)
    >>>print(dict)
    {'name' : 'hy', 'age' : 23, 'sex' : 'male', 'school' : 'ahpu', 'work' : 'student'}
    (2)元组(在小括号内,以逗号分开)
    #元组是不可变类型,赋值后不可变
    >>>tuple = (1,2,3,4,5,6)
    1. 按索引取值
    >>>print(tuple[2])
    3
    2. 切片
    >>>print(tuple[0:6])
    (1,2,3,4,5,6)
    >>>print(tuple[0:6:2])
    (1,3,5)
    3. len()
    >>>print(len(tuple))
    6
    4. 成员运算in 和not in
    >>>print(1 in tuple)
    True
    >>>print(1 not in tuple)
    False
    5. 循环
    >>>for i in tuple:
           print(i)
    (3)文件处理:
    #文件读写基本使用
    #对文本进行操作
    #open(参数1 : 文件名, 参数2 : 操作模式, 参数3 : 指定字符编码)
    # f 称为句柄
    # r: read,只读模式,只能读不能写,文件不存在时报错。
    # w: 只能写,不能读,文件存在的时候回清空文件后再写入内容;文件不存在的时候会创建文件后写入内容。
    # a: 可以追加。文件存在,则在文件的末端写入内容;文件不存在的时候会创建文件后写入内容。
    # b模式是通用的模式,因为所有的文件在硬盘中都是以二进制的形式存储的,需要注意的是:b模式读写文件,一定不能加上encoding参数,因为二进制无法再编码。
    #文件操作的基础模式有三种(默认的操作模式为r模式):
            # r模式为read
            # w模式为write
            # a模式为append
    #文件读写内容的格式有两种(默认的读写内容的模式为b模式):
            # t模式为text
            # b模式为bytes
            #需要注意的是:t、b这两种模式均不能单独使用,都需要与r/w/a之一连用。
    #打开会产生两种资源,一种是python解释器和python文件资源,程序结束python会自动回收
    #另一种是操作系统打开文件的资源,文件打开后操作系统不会自动回收,需要手动回收资源
    1.  文件读写操作
    #写文件 w
    f = open(FilePath, mode = 'wt', encoding = 'utf-8')     #路径前加 r 可将路径中的转义字符变为普通字符
    f.write('hello hy!')
    f.close()     #文件资源回收
    #读文件 r
    f = open(FilePath, 'rt', encoding = 'utf-8')     #默认rt,mode可不写
    file = f.read()
    print(file)
    f.close()
    #文件追加 a
    f = open(FilePath, 'at', encoding = 'utf-8')     #默认at
    f.write('hello yh')
    f.close()
    #写文件与追加的区别:写文件是写入新文件,或覆盖文件原有内容,追加是在已有的文件内容后面添加内容
    #文件处理之上下文管理
    #with自带close()功能,会在文件处理结束后自动调用close()关闭文件
    #写文件
    with open(FilePath, mode = 'wt', encoding = 'utf-8') as f:
        f.write('life is short, u need python!')
    #读文件
    with open(FilePath, mode = 'rt', encoding = 'utf-8') as f:
        file = f.read()
        print(file)
    #文件追加
    with open(FilePath, mode = 'at', encoding = 'utf-8') as f:
        f.write('life is short, u need python!')

    2. 图片与视频读写操作
    #写入图片
    import requests
    pic = requests.get(picPath)           #可从bing搜索图片链接
    with open(picPath, 'wb') as f:
        f.write(pic.content)
    #读图片
    with open(picPath, 'rb') as f:
        file = f.read()
        print(file)
    #图片拷贝操作
    with open(picPath1, 'rb') as f1, open(picPath2, 'wb') as f2:
        pic = f1.read()
        f2.write(pic)
    #视频操作同上
    #一行一行读文件
    with open(picPath1, 'rb') as f1, open(picPath2, 'wb') as f2:
        # f1.read()     #依次打开文件所有内容,若文件的大小超出内存大小会导致内存溢出
        for i in f1:     #一行一行读取文件内容,一行一行写入文件内容,避免内存溢出
            f2.write(i)
     
    (4)函数
    #函数必须先定义,后调用
    #函数的语法
    # def:(全称define),用来声明定义函数关键字
    # 函数名:见名知意
    # ():小括号,存放的是接受外界的参数
    def cup(参数1, 参数2...) :
        '''
        函数声明
        水杯,用来盛水与喝水
        '''
        函数体代码
        return 返回值
    #函数在定义阶段发生的事情
      # 1.先打开 python 解释器
      # 2.加载 python 的 .py 文件
      # 3.python 解释器会检测 py 文件中的语法,但是只会检测 python 语法,不会执行函数体代码
    #例子
    #注册功能
    def register():
        '''
        注册功能
        '''
        while True:
            user = input('请输入用户名').strip()
            pwd = input('请输入密码').strip()
            re_pwd = input('请确认密码').strip()
           
            #判断两次密码是否相同
            if pwd == re_pwd:
               
                #格式化字符串的三种方法
                user_info1 = '用户名:%s,密码:%s' % (user,pwd)
                user_info2 = '用户名:{},密码:{}'.format(user,pwd)
               
                #字符串前写一个 f 相当于后面调用 format     python3.6以上可用此方式
                user_info3 = f'用户名:{user},密码:{pwd}'
               
                #把用户信息写入文件
                with open(f'C:/Users/administortra/Desktop/{user}.txt','w',encoding = 'utf-8') as f:
                    f.write(user_info3)
               
                break
           
            else:
                print('两次密码不一致,请重新输入!')
               
    #调用函数   函数名() 即调用函数
    register()
     
     
     
  • 相关阅读:
    Python解释器安装
    有钱就放余额宝的人,这个习惯恐怕要改一改!
    这么详细的存储基础知识,你不看看? 会后悔的!
    超全!华为交换机端口vlan详解~
    华为:鸿蒙绝不是安卓换皮!!!
    VS Code 真的会一统江湖吗?
    用户与安全 -(1)Linux用户及组管理
    运维必看!这个技能薪水28.8万,工资竟然还只是零花钱....
    原来 Linux 日志文件系统是这样工作的~
    干货长文:Linux 文件系统与持久性内存介绍
  • 原文地址:https://www.cnblogs.com/jacob1998/p/11013267.html
Copyright © 2020-2023  润新知