zoukankan      html  css  js  c++  java
  • 函数function

    --------------遇到困哪的时候,跟自己说,坚持一下就过去了.

    # def playball():
    # print("拿球")
    # print("叫上小伙伴")
    # print("去球场")
    # print("打球")
    # print("命中三分球")
    # return "没中"
    # print("fell good!")
    #
    # s = playball()
    # print(s)
    # playball()
    # print("------------改天再战------------")

    # def playball():
    # print("拿球")
    # print("叫上小伙伴")
    # print("去球场")
    # print("打球")
    # print("命中三分球")
    # return "没中","好久没用打球","在投一个试试"
    #
    # s = playball()
    # print(s)
    # playball()
    # print("------------改天再战------------")

    #
    # def playball(ball):
    # print("今天我们去打%s吧,OK吗?" % ball)
    #
    # playball("篮球")
    # playball("足球")
    # playball("桌球")
    # playball("排球")
    # playball("网球")

    # ---------------------------------------------------------------------------------#

    # day9 作业及默写
    # 1,整理函数相关知识点,写博客。
    #
    # 1、函数的定义:将可重复使用的,实现某种功能的代码段组织在一起
    # 2、函数的语法:
    # def 函数名(参数):
    # 函数体
    # return
    # 2.1、函数名是一个变量,因此命名规则需遵守变量命名规则
    #
    # 3、函数的调用
    # 函数名()
    # 4、函数的返回值
    # return 可空、None、单个、多个以tuple返回给调用者
    # 5、函数的参数:
    # 函数定义时声明的变量的叫形参
    # 1、位置参数
    # 2、默认参数
    # 函数调用时传给函数的叫实参
    # 1、位置参数
    # 定义多少个,调用时也必须传多少个
    # 2、关键字参数
    # 3、混合参数
    # 位置参数需在关键字参数之前
    # ---------------------

    ##---------------------------------------------------------------------------------#
    # 2,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
    # def check(lst):
    # lst1 = []
    # for i in range(len(lst)):
    # if i % 2 != 0:
    # lst1.append(lst[i])
    # return lst1
    #
    #
    # a = ["j", "k", "a", "c", "b", "d"]
    # b = [4,1,5,2,6,3,7,4,8,5]
    # rst = check(a)
    # rst1 = check(b)
    #
    # print(rst)
    # print(rst1)
    # ---------------------------------------------------------------------------------#
    # 3,写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。

    # def check_len(str):
    # if len(str)> 5:
    # print("ok")
    # else:
    # print("长度不大于5")
    # return "判断完成"
    #
    # # a="fashlfk;ahs+"
    # # a1 = ["a","2",(2,5)]
    # a2 = (1,"cc",[5,20,13,1,4],6,77,(8,99))
    #
    # # res = check_len(a)
    # # res1 = check_len(a1)
    # res2 = check_len(a2)
    #
    # # print(res)
    # # print(res1)
    # print(res2)
    ##---------------------------------------------------------------------------------#
    # 4,写函数,检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者。
    # def check_len(lst):
    # if len(lst) > 2:
    # print("ok")
    # return c
    # else:
    # print("长度不大于2")
    # return "判断失败"
    #
    # a2 = [1, "cc", [5, 20, 13, 1, 4], 6, 77, (8, 99)]
    # c = [a2[0], str(a2[1])]
    # res2 = check_len(a2)
    #
    # print(res2)
    # ---------------------------------------------------------------------------------#
    # 5,写函数,计算传入函数的字符串中, 数字、字母、空格 以及 其他内容的个数,并返回结果。
    # def count_num(str):
    # num = []
    # alpha = []
    # space = []
    # other = []
    # for i in str:
    # if i.isdigit():
    # num.append(i)
    # elif i.isalpha():
    # alpha.append(i)
    # elif i.isspace():
    # space.append(i)
    # else:
    # other.append(i)
    # sum = [len(num), len(alpha), len(space), len(other)]
    # return sum
    #
    # s = "1a-2b 3cc 4#d 6+7efg %5 5*3+0 "
    # res = count_num(s)
    # print(res)
    # ---------------------------------------------------------------------------------#
    # 6,写函数,接收两个数字参数,返回比较大的那个数字。
    # def big(a,b):
    # if a>b:
    # return a
    # else:
    # return b
    # res = big(25,36)
    # print(res)

    ##---------------------------------------------------------------------------------#
    # 7,写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    # dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    # PS:字典中的value只能是字符串或列表
    # dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    #
    # def check(dic):
    # b =[]
    # c =[]
    #
    # for v in dic.values():
    # b.append(v)
    # print(b)
    # print(len(b))
    #
    # for i in range(2):
    # print(i)
    #
    # if len(b[i]) > 2:
    # b[i] = [str(b[i][0]),str(b[i][1])]
    # c.append(b[i])
    # print(c)
    # return c
    #
    # res = check(dic)
    # print(res)
    # ---------------------------------------------------------------------------------#
    # 8,写函数,此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,此字典的键值对为此列表的索引及对应的元素。
    # 例如传入的列表为:[11,22,33] 返回的字典为 {0:11,1:22,2:33}。
    # def lis_to_dic(s):
    # dic = {}
    # if type(s) == list:
    # for i in range(len(s)):
    # dic[i] = s[i]
    # return dic
    # else:
    # print("您输入的不是列表")
    #
    # s = ["a","b","c"]
    # res = lis_to_dic(s)
    # print(res)
    # ---------------------------------------------------------------------------------#
    # 9,写函数,函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中,
    # 此函数接收到这四个内容,将内容追加到一个student_msg文件中。
    # def data(name,sex,age,education):
    # f = open("student_msg","a+",encoding="utf8")
    # f.write(name+'-'+sex+'-'+str(age)+'-'+education+ ' ')
    # f.flush()
    # f.close()
    # n = input("请输入名字:")
    # a = input("请输入年龄:")
    # g = input("请输入性别:")
    # e = input("请输入学历:")
    # data(n,a,g,e)

    # data("铅笔小学","boy","六","undergruate")
    # ---------------------------------------------------------------------------------#
    # 10,对第9题升级:支持用户持续输入,Q或者q退出,性别默认为男,如果遇到女学生,则把性别输入女。

    # def data(name, age, education,sex="男"):
    # f = open("student_msg.txt", "a", encoding="utf8")
    # f.write(name + '-' + sex + '-' + str(age) + '-' + education+ ' ')
    # f.flush()
    # f.close()
    #
    # while 1:
    # s = input("按回车输入内容,按Q退出:")
    # if s.upper() == "Q":
    # break
    # else:
    # n = input("请输入名字:")
    # a = input("请输入年龄:")
    # g = input("请输入性别:")
    # e = input("请输入学历:")
    # if g == "":
    # data(n, a, e)
    # else:
    # data(n, a, e, g)

    # ---------------------------------------------------------------------------------#
    # 11,写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成文件的修改操作(升级题)。
    # import os
    # def func(f_name,data,data_new):
    # with open(f_name,"r",encoding="utf8") as f1,
    # open(f_name+"_副本","w",encoding="utf8") as f2:
    # for line in f1:
    # s = line.replace(data,data_new)
    # f2.write(s)
    # os.remove(f_name)
    # os.rename(f_name+"_副本",f_name)
    #
    # func("f_name.txt","9","666")
    ##---------------------------------------------------------------------------------#
    # 12,写一个函数完成三次登陆功能,再写一个函数完成注册功能(升级题)
    u_nam = "jien"
    p_wrd = 123

    def login():
    count = 3
    while count > 0 :
    um = input("请输入账号名称:")
    pd = int(input("请输入密码:"))
    if um == u_nam and pd == p_wrd:
    print("登录成功")
    break
    else:
    print("账号或密码验证失败")
    count -= 1
    print("剩余%s机会" % count)

    if count == 0:
    break
    login()

    def digest():
    while 1:
    um2 = input("请输入账号名称:")
    pd2 = int(input("请输入密码:"))
    if um2 == u_nam:
    print("账号已经存在,重新输入")
    continue
    else:
    print("注册成功")
    break

    digest()







    ##---------------------------------------------------------------------------------#
    # u_name = "jien"
    # p_word = 123
    #
    # def login():
    # for i in range(0,3):
    # name = input('请输入账号:')
    # if name == u_name:
    # psw = int(input('请输入密码:'))
    # if psw == p_word:
    # print('-->登录成功<--')
    # return
    # else:
    # print('-->密码错误<--')
    # if i == 2:
    # print('您的机会已用完,请12小时之后再试')
    # else:
    # print('您还有%s次机会' % (2 - i))
    # else:
    # print('-->账号不存在<--')
    # if i == 2:
    # print('您的机会已用完,请24小时之后再试')
    # else:
    # print('您还有%s次机会' % (2 - i))
    # res = login()
    #
    #
    # def regist():
    # while 1:
    # print('-->请注册<--')
    # re_name = input('请输入账号:')
    # if re_name == u_name:
    # print('-->账户已存在<--')
    # continue
    # else:
    # re_psw = int(input('请输入密码:'))
    # return ('-->注册成功<--')
    # re = regist()
    # print(re)


    # 明日默写。
    # ①,return的作用。
    # ②,传参的几种方法,每个都简单写一个代码。
    # 如,实参,按位置传参。

    # def func(x,y):
    # Pass
    # func(‘a’,’b’)
  • 相关阅读:
    Android Api 检查參数状态Api
    【Jquery】prop与attr的差别
    【CODEFORCES】 A. Dreamoon and Sums
    使用&lt;jsp:include&gt;,不想写死URL,动态生成URL的解决的方法
    android.app.Activity 的介绍
    字符变换
    android之获取屏幕的宽度和高度
    2015跑过的路
    hdu5418--Victor and World(floyd+状压dp)
    解决PL/SQL Developer 连接oracle 11g 64位中的问题
  • 原文地址:https://www.cnblogs.com/dealdwong2018/p/9873481.html
Copyright © 2011-2022 走看看