zoukankan      html  css  js  c++  java
  • python-code-09

    函数练习:
    1、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作
    方式一:小文件修改
    def change_file(file,old,new):
        with open(file,'rt',encoding='utf-8') as read_f:
            data = read_f.read()
            data = data.replace(old,new)
        with open(file,'wt',encoding='utf-8') as write_f:
            write_f.write(data)
        print('success')
    file_u = input('filename>>: ')
    old_u = input('old>>: ')
    new_u = input('new>>: ')
    change_file(file_u,old_u,new_u)
    
    方式二:大文件修改
    def change_file(file,old,new):
        import os
        file1 = file+'.swap'
        with open(file,'rt',encoding='utf-8') as read_f,
                open(file1,'wt',encoding='utf-8') as write_f:
            for line in read_f:
                line = line.replace(old,new)
                write_f.write(line)
        os.remove(file)
        os.rename(file1,file)
        print('success')
    file_u = input('filename>>: ')
    old_u = input('old>>: ')
    new_u = input('new>>: ')
    change_file(file_u,old_u,new_u)
    View Code

    2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
    def str_count():
        str_u = input('请输入>>: ')
        digit_count = 0
        alpha_count = 0
        space_count = 0
        other_count = 0
        for i in str_u:
            if i.isdigit():
                digit_count += 1
            elif i.isalpha():
                alpha_count += 1
            elif i.isspace():
                space_count += 1
            else:
                other_count += 1
        print('数字:%s 字母:%s 空格:%s 其他:%s' % (digit_count, alpha_count, space_count, other_count))
    str_count()
    View Code

    3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
    def func3(seq):
        if len(seq) > 5:
            return True
        else:
            return False
    print(func3([1,2,3,4,5,6]))
    View Code

    4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    def func1(seq):
        if len(seq) > 2:
            return seq[0:2]
    print(func1([1,2,3,4]))
    View Code

    5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
    def func2(seq):
        return seq[::2]
    print(func2([1,2,3,4,5,6,7]))
    View Code

    6、写函数,检查字典的每一个value的长度, 如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]}
    PS:字典中的value只能是字符串或列表
    dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]}
    def func6(seq):
        for k in seq:
            if len(seq[k]) > 2:
                seq[k] = seq[k][:2]
        return seq
    print(func6(dic))
    View Code

    7、编写认证功能函数,注意:后台存储的用户名密码来自于文件
    name_inp = input('请输入用户名>>: ').strip()
    pwd_inp = input('请输入密码>>: ')
    def auth(name_inp,pwd_inp):
        with open('db.txt','rt',encoding='utf-8') as f:
            for line in f:
                line = line.strip('
    ').split(':')
                if name_inp == line[0] and pwd_inp == line[1]:
                    print('认证成功')
                    break
            else:
                print('用户名或密码不正确')
    
    auth(name_inp,pwd_inp)
    View Code

    8、编写注册功能函数,将用户的信息储存到文件中
    name_list = []
    with open('db.txt', 'rt', encoding='utf-8') as f:
        for line in f:
            line = line.strip('
    ').split(':')
            name_list.append(line[0])
    while True:
        name_inp = input('请输入用户名>>: ').strip()
        if name_inp in name_list:
            print('用户名已存在')
            continue
        pwd_inp = input('请输入密码>>: ')
        pwd_inp_check = input('确认密码>>: ')
        if pwd_inp != pwd_inp_check:
            print('两次密码不一致')
        else:
            break
    
    def register(name_inp,pwd_inp):
        '''
        注册功能
        :param name_inp:
        :param pwd_inp:
        :return:
        '''
        with open('db.txt','at',encoding='utf-8') as f:
            f.write('%s:%s
    ' %(name_inp,pwd_inp))
            print('注册成功')
    register(name_inp,pwd_inp)
    View Code

    9、编写查看用户信息的函数,用户的信息是事先存放于文件中的
    name_inp = input('请输入用户名>>: ').strip()
    pwd_inp = input('请输入密码')
    def view(name_inp,pwd_inp):
        '''
        查看用户信息
        :param name_inp:
        :param pwd_inp:
        :return:
        '''
        with open('db.txt','rt',encoding='utf-8') as f:
            for line in f:
                line = line.strip('
    ').split(':')
                if name_inp == line[0] and pwd_inp == line[1]:
                    print('用户名:%s 密码:%s' %(name_inp,pwd_inp))
                    break
    view(name_inp,pwd_inp)
    View Code

    明日默写
    1、修改文件的两种方式
    with open('db','rt',encoding='uf-8') as read_f:
        data = read_f.read()
        data = data.replace('old','new')
    with open('db','wt',encoding='utf-8') as write_f:
        write_f.write(data)
    
    import os
    with open('db','rt',encoding='utf-8') as f2,
            open('db.swap','wt',encoding='utf-8') as f3:
        for line in f2:
            f3.write(line.replace('old','new'))
    os.remove('db')
    os.rename('db.swap','db')
    View Code

    2、注册功能
    name_inp = input('username>>: ').strip()
    pwd_inp = input('password>>: ')
    pwd_inp2 = input('password>>: ')
    if pwd_inp == pwd_inp2:
        with open('db','at',encoding='utf-8') as f:
            f.write('%s:%s
    ' %(name_inp,pwd_inp))
    View Code

    3、认证功能
    name_inp = input('name>>: ').strip()
    pwd_inp = input('password>>: ')
    with open('db','rt',encoding='utf-8') as f:
        for line in f:
            line = line.strip('
    ').split(':')
            if name_inp == line[0] and pwd_inp == line[1]:
                print('验证通过')
                break
        else:
            print('用户名或密码错误')
    View Code
  • 相关阅读:
    java 泛型详解
    Vector源码解析
    栈的应用 函数调用
    java中ArrayList 遍历方式、默认容量、扩容机制
    java代码实现自定义栈 + 时间复杂度分析
    mySql分页Iimit优化
    Mybatis 动态SQL注解 in操作符的用法
    设计模式之 外观模式
    设计模式之 装饰器模式
    设计模式之 组合模式
  • 原文地址:https://www.cnblogs.com/xujinjin18/p/9157965.html
Copyright © 2011-2022 走看看