zoukankan      html  css  js  c++  java
  • 函数的难度操作

    # # 2,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
    # def lst_new(s):
    #     count=0
    #     lst=[]
    #     for i in s:
    #         if count%2==1:
    #             lst.append(i)
    #         count+=1
    #     return lst
    # ret=lst_new((15,6,"a",7,8,90))
    # print(ret)
    #3.写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5:
    # def judge(s):
    #     while len(s)>5:
    #         return "用户传入的对象{}长度大于5".format(s)
    #     else:
    #         return "用户传入的对象{}长度小于5".format(s)
    # ret=judge((1,4,6,7,8,4,))
    # print(ret)
    #4.写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。
    # def length(s):
    #     if len(s)>2:
    #         return s[0],s[1]
    # ret=length([2,3,4,5])
    # print(ret)
    #5.写函数,计算传入函数的字符串中, 数字、字母、空格 以及 其他内容的个数,并返回结果。
    # def count(s):
    #     num_count=0
    #     alph_count=0
    #     space_count=0
    #     other_count=0
    #     for i in s:
    #         if i.isalpha():
    #             alph_count+=1
    #         if i.isdigit():
    #             num_count+=1
    #         if i.isspace():
    #             space_count+=1
    #         else:
    #             other_count+=1
    #     return "数字有{}个,字母有{}个,空格有{}个,其他有{}个".format(num_count,alph_count,space_count,other_count)
    # ret=count("ahjo__123o23a")
    # print(ret)
    #6.写函数,接收两个数字参数,返回比较大的那个数字。
    # def max(a,b):
    #     if a>b:
    #         return a
    #     return b
    # print(max(23,21))
    # def max(a,b):
    #     c=a if a>b else b
    #     return c
    # print(max(2,3))
    #7.写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    	# dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    # 	# PS:字典中的value只能是字符串或列表
    # def s(dic):
    #     for k,v in dic.items():
    #         if len(v)>2:
    #             dic[k]=v[0:2]
    #     return dic
    # print(s({"k1": "v1v1", "k2": [11,22,33,44]}))
    #8.写函数,此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,
    # 此字典的键值对为此列表的索引及对应的元素。例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。
    # def s(lst):
    #     dic={}
    #     for i in range(len(lst)):
    #         dic.setdefault(i,lst[i])
    #         #dic[i]=lst[i]
    #     return dic
    # print(s([23,34,56,78]))
    #9,写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,
    # 然后将这四个内容传入到函数中,此函数接收到这四个内容,将内容追加到一个student_msg文件中。
    # name=input("请输入名字:")
    # sex=input("请输入性别:")
    # age=input("请输入年龄")
    # recordeducation=input("请输入学历")
    # def s(a,b,c,d):
    #     f=open("student_msg",mode="w",encoding="utf-8")
    #     f.write("姓名:%s,性别:%s,年龄:%s,学历:%s"%(a,b,c,d))
    #     f.flush()
    #     f.close()
    # s(name,sex,age,recordeducation)
    #10,对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。
    # while 1:
    #     content=input("请输入判断是否持续输入内容:")
    #     def s(a, b, c, d="男"):
    #         f = open("student_msg", mode="a+", encoding="utf-8")
    #         f.write("姓名:%s,性别:%s,年龄:%s,学历:%s" % (a,d, b, c))
    #         f.flush()
    #         f.close()
    #     if content.upper()=="Q":
    #         break
    #     else:
    #         name=input("请输入名字:")
    #         sex=input("请输入性别:")
    #         age=input("请输入年龄:")
    #         recordeducation=input("请输入学历:")
    #         if sex=="男":
    #             s(name,age,recordeducation)
    #         if sex=="女":
    #             s(name,age,recordeducation,d="女")
    #11.写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(升级题)。 q
    # 参考:
    # name=input("请输入文件名:")
    # content=input("需要修改的内容:")
    # content1=input("修改后的内容:")
    # def r(n,c,c1):
    #     import os
    #     with open("%s"% n,mode="r",encoding="utf-8")as f,
    # open("%s副本"%n,mode="w",encoding="utf-8")as f1:
    #         s=f.read()
    #         s1=s.replace(c,c1)
    #         f1.write(s1)
    #     os.remove("%s"% n)
    #     os.rename("%s副本"%n,"%s"% n)
    # r(name,content,content1)
    # 12.写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)
    def s():
        count=3
        while count>0:
            username=input("请输入用户名:").strip()
            password=input("请输入密码:").strip()
            if username=="123"and password=="123":
                 print("用户登录失败")
                 break
            else:
                print( "登录成功,剩余%d"% count-1)
            count-=1
            continue
    def s1():
        dic={}
        username1= input("请输入用户名:").strip()
        password1 = input("请输入密码:").strip()
        dic.update({username1: password1})
    s()
    s1()
    
  • 相关阅读:
    CodeForces
    CodeForces
    CodeForces
    HDU 6704 K-th occurrence(后缀数组,主席树,st表,二分)
    AcWing 1004. 品酒大会 (后缀数组,并查集)
    Gym
    codeforces 2100左右的DS题 做题记录
    P4768 [NOI2018] 归程 做题记录
    CSP 2021 智熄记
    「随笔」论打羽毛球的正确姿势
  • 原文地址:https://www.cnblogs.com/zhangdaye/p/9306036.html
Copyright © 2011-2022 走看看