• python练习题-day9


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

    def dan_index(itter):
        li=[]
        for i in range(len(itter)):
            if i%2==1:
                li.append(itter[i])
        return li

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

    def len_itter(itt):
        count=0
        for i in itt:
            count+=1
        if count>5:
            return "{}长度大于5".format(itt)
        else:
            return "%s 长度小于5"%itt
    print(len_itter([3489,34,"hello","myfu","good",78]))

    4、写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。 

    def it_len(itt):
        if len(itt)>2:
            return itt[:2]

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

    def str_count(st):
        s=0
        b=0
        n=0
        for i in st:
            if i.isdigit():
                n+=1
            if i.isalpha():
                s+=1
            if i.isspace():
                b+=1
        return "%s有%s个数字%s字母%s空格"%(st,n,s,b)

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

    def num_cam(a,b):
        if a>b:
            return a
        else:
            return b

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

           dic = {"k1": "v1v1", "k2": [11,22,33,44]}

           PS:字典中的value只能是字符串或列表

    def dic_filter(dic):
        for i,j in dic.items():
            if len(j)>2:
                j=j[:2]
                dic.update({i:j})
        return dic
    print(dic_filter( {"k1": "v1v1", "k2": [11,22,33,44]}))

    8、写函数,此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,此字典的键值对为此列表的索引及对应的元素。例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。

    #方法一
    def list_trans(li):
        dic={}
        for i,j in enumerate(li):
            dic.setdefault(i,j)
        return dic
    print(list_trans([11,22,33]))
    #方法二
    def list_trans(li):
        dic={}
        for i in range(len(li)):
            dic.setdefault(i,li[i])
        return dic
    print(list_trans([11,22,33]))

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

    def stu_add(name,gender,age,education):
        with open("stu_info.txt","r+",encoding="utf-8") as f2:
            data=f2.readline()
            f2.write("
    %s  %s  %s  %s"%(name,gender,age,education))

     10、对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。

    def stu_add():
        with open ("stu_info.txt","r+",encoding="utf-8") as f1:
            while True:
                stu_add=input("请输入用户信息(以空格隔开q或Q退出):").strip()
                if len(stu_add.split())==3:
                    name,age,education=stu_add.split()
                    f1.read()
                    f1.write("
    %s  male  %s  %s"%(name,age,education))
                    continue
                if len(stu_add.split())==4:
                    name,gender,age,education=stu_add.split()
                    f1.read()
                    f1.write("
    %s  %s  %s  %s"%(name,gender,age,education))
                    continue
                if stu_add.upper()=="Q":
                    exit("谢谢使用,正在退出")

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

    def file_modify(filename,contant):
        import os
        contant_new = input("请输入修改后的内容:")
        with open(filename,encoding="utf-8")as f1,open("filename_new","w",encoding="utf-8") as f2:
            for i in f1:
                new_i=i.replace(contant,contant_new)
                f2.write(new_i)
        os.remove(filename)
        os.rename("filename_new",filename)

    12、写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)

    def user_register():
    while True:
    uname=input("请输入注册名:")
    pwd=input("请输入注册密码:")
    pwd_2=input("请确认密码:")
    li=[]
    with open("info_database","r+",encoding="utf-8") as f1:
    data=f1.read()
    data_dic=eval(data)
    for i in data_dic.keys():
    li.append(i)
    if uname in li:
    print("用户名已存在,请重新输入")
    continue
    if pwd==pwd_2:
    print("注册成功")
    data_dic.setdefault(uname,pwd)
    f1.seek(0)
    f1.write(str(data_dic))
    break
    else:
    print("密码不一致请重新输入")
    continue
    def login():
    count=0
    while count<3:
    username=input("请输入用户名:").strip()
    password=input("请输入密码:").strip()
    with open("info_database",encoding="utf-8")as f2:
    data=f2.read()
    dic_data=eval(data)
    for i in dic_data.keys():
    if username==i and password==dic_data[i]:
    print("登录成功")
    exit()
    else:
    print("用户名或密码有误还有%s次机会"%(2-count))
    count+=1
  • 相关阅读:
    sqlserver查询某个数据库有多少个表 ,存过,函数,视图
    C# 金额转为大写金额
    C# TextBox中只能输入数字的几种常用方法(C#)
    C# 设置Excel单元格属性
    MS SQL 维护小记
    webapi demo
    远程 TeamViewer
    https://github.com/
    C# Fun 类似委托
    技术点文章收集
  • 原文地址:https://www.cnblogs.com/fumy/p/10304626.html
Copyright © 2020-2023  润新知