函数的返回值 函数的类型 函数的参数(重要) 利用函数对注册登录代码进行封装 常见的内置函数
今日内容概要
-
函数的返回值
-
函数的类型
-
函数的参数(重要)
-
利用函数对注册登录代码进行封装
-
强调
# 1.定义与调用 定义函数使用关键字def 调用函数需要使用 函数名加括号(如果函数需要参数那么则额外传递参数) # 2.定义阶段 函数在定义阶段只检测函数语法,不执行函数语法 # 3.调用阶段 函数名加括号调用函数的时候才会执行函数体代码
函数的返回值
def index(): print('from index') return 123,456 print('from index') index() # 什么是返回值? """返回值就是执行完了某个方法该方法反馈出来的结果""" # 如何获取返回值? """通过变量名与赋值符号即可 变量名 = 方法/函数() """ # 1.当函数体没有return关键字的时候 默认返回none # res =index() # print(res) # none即为空 # 2.return关键字后面写什么函数就返回什么 # res = index() # print(res) # 123 # 3.return后面跟多个默认值会组成元组的形式返回 # res = index() # print(res) # 4.函数体代码遇到return立刻结束整个函数的运行
函数的类型
# 1.无参函数:函数在定义阶段没有参数,调用阶段也不需要给参数 # def index(): print('from index') # index() # 2.有参函数:函数在定义阶段括号内填写了变量名即需要参数 # def index(x): # print('from index',x) # index(1) # 3.空函数(没有具体的函数体代码) """一般用于前期的架构搭建""" def run(): pass # 补全语法结构 本身没有任何含义 def fight(): pass def talk(): pass:
函数的参数
""" 函数在定义阶段括号内书写的变量名称之为函数的形式参数 简称为形参 函数的形参相当于变量名 函数在调用阶段括号内书写的值称之为函数的实际参数 简称为实参 函数的实参相当于变量值 两者在调用函数的时候临时绑定 函数运行结束分开 def index(name): print(name) index('jason') 实参 name = 'jason' """ # 1.位置参数:按照位置一一对应传值 多一个不行少一个也不行 def index(x,y): print(x,y) # index() # 不传不可以 # index(1) # 传少了也不行 # index(1,2) # 个数正确可以 # index(1,2,3,4,5,6,7) # 传多了也不行 # 2.关键字参数:指名道姓的给形参传值 可以打破位置限制 # index(y=111, x=222) # index(111, y=222) # index(111, x=222) # 报错 # print(y=111,222) # 报错 位置参数一定要在关键字参数的前面 # 3.默认参数:函数在定义阶段就给形参赋值了 # def register(name, age, gender='male'): # print(name, age, gender) '''默认参数不给值的情况下就使用默认的 给了就使用给了的''' # register('jason', 18) # register('tony', 28) # register('kevin', 38) # register('huahua', 18, 'female') # 4.可变长参数(使用频率最高) '''函数如何做到无论接收多少个位置参数都可以正常运行 *在形参中使用 会接收多余的位置参数组织成元组的形式赋值给*后面的变量名 ''' # def func(a, *b): # print(a, b) # func(1,2,3,4,5,6,7,8,9,10) # func(1) """函数如何做到无论接收多少个关键字参数都可以正常运行 **在形参中使用 会接收多余的关键字参数组织成字典的形式赋值给**后面的变量名 """
# def func(a,**b): # print(a,b) # func(a=1,b=2,c=3,d=4,e=5) # func(a=1) # 小练习 定义一个函数 该函数无论接收多少个位置参数还是关键字参数都可以正常运行de def func(*a, **b): print(a, b)
""" 约定俗成 针对形参中的可变长参数 变量名推荐使用 *args **kwargs def login(*args, **kwargs): pass """
*与**在实参中的作用
l = [11, 22, 33, 44, 55] d = {'username': 'jason', 'pwd': 123, 'hobby': 'read'} def func(*args, **kwargs): print(args, kwargs) """ *在实参中使用 会将列表/元组内的元素打散成一个个位置参数传递给函数 """ func(*l) # func(11,22,33,44,55) """ **在实参中使用 会将字典内的元素打散成一个个关键字参数传递给函数 """ func(**d) # func(username='jason',pwd=123,hobby='read')
def register(): username = input('username>>>:').strip() password = input('password>>>:').strip() # 校验用户名是否存在 with open(r'userinfo.txt', 'r', encoding='utf8') as f: for line in f: real_name = line.split('|')[0] if real_name == username: print('用户名已存在') return # 将用户名和密码写入文件 with open(r'userinfo.txt', 'a', encoding='utf8') as f: f.write('%s|%s\n' % (username, password)) print('%s注册成功' % username) def login(): username = input('username>>>:').strip() password = input('password>>>:').strip() with open(r'userinfo.txt', 'r', encoding='utf8') as f: for line in f: real_name, real_pwd = line.split('|') if real_name == username and password == real_pwd.strip('\n'): print('登录成功') return print('用户名或密码错误') func_dict = {'1': register, '2': login } while True: print(""" 1 注册 2 登录 """) choice = input('请输入执行功能编号>>>:').strip() if choice in func_dict: func_name = func_dict.get(choice) func_name() else: print('请输入正确的编号')
"""
将面条版代码封装成函数版
1.先写def和函数名
2.将代码缩进
3.查看内部需要哪些数据
4.在形参中定义出来即可
尽量做到一个函数就是一个具体的功能
"""
def auth_username(username): # 校验用户名是否存在 with open(r'userinfo.txt', 'r', encoding='utf8') as f: for line in f: real_name = line.split('|')[0] if real_name == username: print('用户名已存在') return True return False def get_info(): username = input('username>>>:').strip() password = input('password>>>:').strip() return username, password def write_data(username, password): # 将用户名和密码写入文件 with open(r'userinfo.txt', 'a', encoding='utf8') as f: f.write('%s|%s\n' % (username, password)) print('%s注册成功' % username) def register(): username, password = get_info() # 调用校验用户名是否存在的函数 is_exist = auth_username(username) if is_exist: return write_data(username, password) def login(): username, password = get_info() with open(r'userinfo.txt', 'r', encoding='utf8') as f: for line in f: real_name, real_pwd = line.split('|') if real_name == username and password == real_pwd.strip('\n'): print('登录成功') return print('用户名或密码错误') func_dict = {'1': register, '2': login } while True: print(""" 1 注册 2 登录 """) choice = input('请输入执行功能编号>>>:').strip() if choice in func_dict: func_name = func_dict.get(choice) func_name() else: print('请输入正确的编号')
内置参数
# print(abs(-111)) # print(all([1, 2, 3, 4, 0])) # print(any([1, 2, 3, 4, 0])) # print(bin(100)) # print(oct(100)) # print(hex(100)) # name = 'jason' # def index(): # pass # 判断变量是否可以调用 # print(callable(name)) # False # print(callable(index)) # True # 返回ASCII码中数字对应的字符 # print(chr(65)) # A 65-90 # print(chr(122)) # z 97-122 # format字符串格式化输出 # print('my name is {} and my age is {}'.format('jason',18)) # print('my name is {0} {1} {1} and my age is {1} {0}'.format('jason',18)) # print('my name is {name} {name} {age} and my age is {age}'.format(name='jason',age=18)) # l = ['jason', 'kevin', 'tony', 'jerry', 'tom'] # count = 1 # for name in l: # print(count,name) # count += 1 # for i, name in enumerate(l, 10): # print(i, name) # l = [11, 22, 33, 44, 55, 66, 77, 88, 99] # print(max(l)) # print(min(l)) # print(sum(l))



浙公网安备 33010602011771号