老男孩Python全栈学习 S9 日常作业 009

1、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

def func1(List):
    List2 = []
    for num in range(len(List)):
        if num % 2 != 0:
            List2.append(List[num])
    return List2
View Code

2、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。

def fun1(Object):
    if type(Object) != tuple and type(Object) != list and type(Object) != str:
        print('您传入的参数有误')
        return 1
    if len(Object) > 5:
        print('您的参数长度挺大')
    else:
        print('太他吗短了~')
    return
View Code

3、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

def func1(List):
    if len(List) > 2:
        return List[0:2]
View Code

4、写函数,计算传入字符串中[数字]、[字母]、[空格] 以及 [其他]的个数,并返回结果。

def func1(Str):
    Dict = {'数字':0,'字母':0,'空格':0,'其他':0}
    for i in Str:
        if i.isdigit():
            Dict['数字'] = Dict['数字'] + 1
        elif i.isalpha():
            Dict['字母'] = Dict['字母'] + 1
        elif i.isspace():
            Dict['空格'] = Dict['空格'] + 1
        else:
            Dict['其他'] = Dict['其他'] + 1
    return Dict
View Code

5、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容,并返回结果。

def func1(Object):
    if isinstance(Object,(str,list,tuple)):
        for i in Object:
            i = str(i)
            if i.isspace():
                print('包含空格')
                return
View Code

6、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

def func1(Object):
    if isinstance(Object,dict):
        for i,k in Object.items():
            if len(k) > 2:
                Object[i] = k[0:2]
        return Object
View Code

7、写函数,接收两个数字参数,返回比较大的那个数字。

def func1(n,m):
    if isinstance(n,int) and isinstance(m,int):
        if n > m:
            return n
        else:
            return m
    print('参数有误,请重新传入')
    return 1
View Code

8、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(进阶)。

def func(filename,old,new):
    with open(filename, encoding='utf-8') as f, open('%s.bak'%filename, 'w', encoding='utf-8') as f2:
        for line in f:
            if old in line:
                line = line.replace(old,new)
            f2.write(line)
    import os
    os.remove(filename)
    os.rename('%s.bak'%filename, filename)
View Code

9、写一个函数完成三次登陆功能,再写一个函数完成注册功能

def regis():
    N = 0
    while N < 3:
        Input_Username = input('请输入您要注册的账号:')
        Input_Password = input('请输入您要注册的密码:')
        if Input_Username.isdigit() and len(Input_Username) > 0 and len(Input_Username) < 11:
            if Input_Password.isalnum() and len(Input_Password) > 0 and len(Input_Password) < 16:
                with open('Username','a',encoding='utf-8') as File:
                    File.write('
'+ Input_Username + ',' + Input_Password)
            print('注册成功,您可以登陆了')
            break
        else:
            print('注册失败,请重新注册!')
        N += 1
    else:
        print('注册失败!')

def login():
    Count = 0
    while Count < 3:
        Input_u = input('请输入您要登陆的用户名:')
        Input_p = input('请输入您要登陆的密码:')
        if Input_u.isdigit() and len(Input_u) > 0 and len(Input_u) < 11:
            if Input_p.isalnum() and len(Input_p) > 0 and len(Input_p) < 16:
                with open('Username',encoding='utf-8') as File:
                     for i in File.readlines():
                        List = i.split(',')
                        if Input_u == List[0] and Input_p == List[1]:
                            print('登陆成功,欢迎您!')
                            break
                     else:
                         print('你输错啦,请重新输入')
                         continue
                     break
        else:
            print('你输错啦,请重新输入')
        Count += 1
View Code

10、写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中,此函数接收到这四个内容,将内容追加到一个student_msg文件中。

def func(name,sex,age,edu):
    Edu = ['小学','初中','高中','专科','本科','硕士','博士']
    if name.isalpha() and sex in ['',''] and age.isdigit() and edu in Edu:
        with open('student_msg','a+',encoding='utf-8') as File1:
            File1.write("%s	%s	%s	%s
"%(name,sex,age,edu))
            return '感谢您的配合'
    else:
        print('您输入的内容有误')
        return 'Good-bye'
View Code

11、在上面题目的基础上添加后续功能:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。

def func(name,age,edu,sex = ''):
    Edu = ['小学','初中','高中','专科','本科','硕士','博士']
    if name.isalpha() and age.isdigit() and edu in Edu and sex in ['','']:
        with open('student_msg','a+',encoding='utf-8') as File1:
            File1.write("%s	%s	%s	%s
"%(name,sex,age,edu))
            return '感谢您的配合'
    else:
        print('您输入的内容有误')
        return 'Good-bye'

while 1:
    name = input('请输出您的名字:')
    age = input('请输出您的年龄:')
    edu = input('请输出您的学历:')
    sex =  input('请输出您的性别:')
    func(name,sex,age,edu)
    choose = input('是否继续,按任意键回车,退出请按Q:')
    if choose.upper() == 'q'.upper():
        print('感谢您的配合!Good-bye')
        break
View Code
原文地址:https://www.cnblogs.com/guge-94/p/10575441.html