函数

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)




posted @ 2017-09-21 20:59  美美的黑天鹅  阅读(120)  评论(0编辑  收藏  举报