python-code-04

函数练习:
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、编写认证功能函数,注意:后台存储的用户名密码来自于文件
def auth():
    name_inp = input('username>>: ').strip()
    pwd_inp = input('password>>: ')
    with open('db','rt',encoding='utf-8') as f:
        for line in f:
            line = line.strip('\n').split(':')
            if name_inp == line[0] and pwd_inp == line[1]:
                print('验证成功')
                break
        else:
            print('用户名或密码错误')
auth()
View Code

8、编写注册功能函数,将用户的信息储存到文件中
def func8():
    name_inp = input('username>>: ').strip()
    pwd_inp = input('password>>: ')
    pwd_inp2 = input('password>>: ')
    if pwd_inp == pwd_inp2:
        print('注册成功')
        with open('db','at',encoding='utf-8') as f1:
            f1.write('%s:%s\n' %(name_inp,pwd_inp))
    else:
        print('两次密码不一致')
func8()
View Code

9、编写查看用户信息的函数,用户的信息是事先存放于文件中的
def func9():
    name = input('username>>: ').strip()
    with open('db','rt',encoding='utf-8') as f:
        for line in f:
            line = line.strip('\n').split(':')
            if name == line[0]:
                print(line)
                break
        else:
            print('用户不存在')
func9()
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\n' %(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('\n').split(':')
        if name_inp == line[0] and pwd_inp == line[1]:
            print('验证通过')
            break
    else:
        print('用户名或密码错误')
View Code

猜你喜欢

转载自www.cnblogs.com/xujinjin18/p/9157965.html