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)




  • 相关阅读:
    Java实现第八届蓝桥杯字母组串
    Java实现第八届蓝桥杯正则问题
    Java实现第八届蓝桥杯方格分割
    Java实现第八届蓝桥杯方格分割
    经典SQL语句大全(绝对的经典)
    SQL脚本
    非常有用的sql脚本
    代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)
    搭建MySQL高可用负载均衡集群
    MySQL——修改root密码的4种方法(以windows为例)
  • 原文地址:https://www.cnblogs.com/cuixn/p/7571290.html
Copyright © 2011-2022 走看看