一、函数的调用
二、函数的作用域
附带代码:
# 函数之间的调用
def eat(name, food):
print("{}喜欢吃食物:{}".format(name, food))
def offer(offer_name, money):
print("{}拿到一个{}k的offer".format(offer_name, money))
eat(offer_name, food="香蕉")
offer("开始", 15)
# 函数的作用域
# 全局能获取局部变量吗?不能!!!
def add(a, b):
c = a + b
c += 5
return c
print(c)
# 局部能获取全局变量吗?能!!!
c = 10
def add(a, b):
return c + a + b
print(add(2, 3))
# 全局能修改局部变量吗?不能!!!
# 局部能修改全局变量吗?不能!!!
c = 10
def add(a, b):
c += 2
return c + a + b
print(add(2, 3))
# global 声明全局变量的关键字,不要轻易使用
c = 10
def add(a, b):
global c # 全局变量c
c += 2
return c + a + b
add(2, 3)
print("最后的c", c)
作业:
1, 定义一个函数,传入一个字典和字符串,判断字符串是否为字典中的值,如果字符串不在字典中,则添加到字典中,并返回新的字典 2, 通过定义一个计算器函数,调用函数传递两个参数,然后提示选择【1】加 【2】减【3】乘 【4】除 操作,选择之后返回对应操作的值。 3, 一个足球队在寻找年龄在15岁到22岁的女孩做拉拉队员(包括15岁和22岁)加入。编写一个程序,询问用户的性别和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次后,输出满足条件的总人数。 4数据转换 # 有一组用例数据如下: cases = [ ['case_id', 'case_title', 'url', 'data', 'excepted'], [1, '用例1', 'www.baudi.com', '001', 'ok'], [4, '用例4', 'www.baudi.com', '002', 'ok'], [2, '用例2', 'www.baudi.com', '002', 'ok'], [3, '用例3', 'www.baudi.com', '002', 'ok'], [5, '用例5', 'www.baudi.com', '002', 'ok'], ] # 要求一:把上述数据转换为以下格式 res1 = [ {'case_id': 1, 'case_title': '用例1', 'url': 'www.baudi.com', 'data': '001', 'excepted': 'ok'}, {'case_id': 4, 'case_title': '用例4', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'}, {'case_id': 2, 'case_title': '用例2', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'}, {'case_id': 3, 'case_title': '用例3', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'}, {'case_id': 5, 'case_title': '用例5', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'} ] # 要求二:把上面转换好的数据中case_id大于3的用例数据获取出来,得到如下结果 res = [ {'case_id': 4, 'case_title': '用例4', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'}, {'case_id': 5, 'case_title': '用例5', 'url': 'www.baudi.com', 'data': '002', 'excepted': 'ok'} ]
上期答案:
# 一、定义函数:将用户输入的所有数字相乘之后对20取余数 # 用户输入的数字个数不确定 # def multiply_module(numbers): # """ # 数据相乘 % 20. # numbers:列表 # """ # start_number = 1 # for i in numbers: # start_number *= int(i) # module = start_number % 20 # return module # # # # input_numbers = input("请输入所有的数字,以逗号隔开:(4,5,6,7)") # numbers = input_numbers.split(",") # print(multiply_module(numbers)) # 二、编写函数,检查传入列表的长度,如果大于2,那么仅仅保留前两个长度的内容,并将新内容返回 """ def list_length(my_list): if len(my_list) > 2: return my_list[:2] return my_list m_list = [2, 3, 5] print(list_length(m_list)) """ # 三、列表去重 # 定义一个函数 def remove_element(m_list):,将列表[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]去除重复元素 """ # 方法一: def remove_element(m_list): new_list = list(set(m_list)) return new_list my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1] print(remove_element(my_list)) # 方法二:借助for循环 def remove_element(my_list): m_list = [] # my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1] for i in my_list: if i not in m_list: m_list.append(i) return m_list my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1] print(remove_element(my_list)) """ # 四、编写如下程序 # 尝试函数封装: # 输入一个人的身高(m)和体重(kg),根据BMI公式(体重除以身高的平方)计算他的BMI指数 # a.例如:一个65公斤的人,身高是1.62m,则BMI为 : 65 / 1.62 ** 2 = 24.8 # b.根据BMI指数,给与相应提醒 # 低于18.5: 过轻 18.5-25: 正常 25-28: 过重 28-32: 肥胖 高于32: 严重肥胖 """ def BMI_index(m, k): BMI = k / (m ** 2) if BMI < 18.5: return "过轻" elif BMI > 18.5 and BMI < 25: return "正常" elif BMI > 25 and BMI < 28: return "过重" elif BMI > 28 and BMI < 32: return "肥胖" elif BMI > 32: return "严重肥胖" print(BMI_index(168, 62)) """