zoukankan      html  css  js  c++  java
  • 函数

    1、组织结构混乱,可读性差
    2、代码冗余
    3、无法统一管理,维护难度极大
    函数的使用必须 先定义在使用

    #函数分类
    1、内置函数:Python解释器自带的函数,Python解释器启动就会定义好这些参数
    len()
    max()
    min()
    sum()


    #2、自定义函数
    语法:
    def 函数名(参数1,参数2,...):
    '注释信息'
    函数体
    return 返回值
    # ##定义阶段
    # def tell_tag():
    # print('========')
    # def tell_msg(msg):
    # print(msg)
    # ##调用阶段
    # tell_tag()
    # tell_tag()
    # tell_msg('hello word')
    # tell_tag()
    # tell_tag()
    --------------------------------
    ##函数在定义阶段,只检测语法,不执行代码
    def func():
    print('aaaaa')
    yy
    # dd
    ##调用阶段 报错
    func()
    ------------------------------
    #定义函数
    def func():
    print('aaaaa')
    yy
    #调用阶段 没问题
    def yy():
    print('ert')
    yy()


    # 无参
    def man():
    while True:
    user = input('>>:')
    passwd = input('>>:')
    #if len(user) == 0:continue
    if not user:continue
    res=auth(user,passwd)
    if res:
    print('login successful')
    else:
    print('login err')

    # 有参:函数体的代码,需要外部传入的值
    def auth(user,passwd):
    if user == 'egon' and passwd == '123':
    return True
    else:
    return False
    man()

    # 空函数
    def foo():
    pass
    练习题
    2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
    # def check_str(msg):
    # res={
    # 'num':0,
    # 'string':0,
    # 'space':0,
    # 'other':0,
    # }
    # for s in msg:
    # if s.isdigit():
    # res['num']+=1
    # elif s.isalpha():
    # res['string']+=1
    # elif s.isspace():
    # res['space']+=1
    # else:
    # res['other']+=1
    # return res
    # res=check_str('hello name:aSB password:a:alex3714')
    # print(res)




  • 相关阅读:
    C/C++产生随机数
    BNUOJ34973Liserious战队
    oracle ebs 12.20 安装成功其过程失败日记及总结(1)
    HDU 2544 最短路 SPFA 邻接表 模板
    GridView编辑删除操作
    Hibernate_10_继承的例子_单表
    String不变性
    Mac在结构quick cocos2d-x编译环境
    Hash散列算法 Time33算法
    南京地图南京全套的卫星地图下载 百度高清卫星地图包括道路、标签信息叠加
  • 原文地址:https://www.cnblogs.com/cuixn/p/7571290.html
Copyright © 2011-2022 走看看