# 定义一个函数 使用方法相比java 方法 简单
# 因为是解释型语言 所以方法函数这些就尽量写在前面
# def 函数名称(形参,形参...):
# 函数体
# return xxxx 如果需要返回数据
def print_poetry():
print("床前明月光")
print("疑似地上霜")
print("举头望明月")
print("疑似地上霜")
return "李白"
author = print_poetry()
print("作者是{name}".format(name=author))
# 函数设置默认值 与 关键字传参
# 可以在方法后面添加默认值 但是有默认值的参数必须在没有默认值参数的后面
# 在方法参数多的情况后可以 声明参数的变量 指定该参数对应方法的名称 这样也便于代码的阅读
def health_check(name, height, weight, h, age="13"):
print(age)
print(height)
print("您的健康状况良好")
return "okk"
print(health_check(name="张三", height="123", weight="123", h="123"))
# 在参数多的情况下 觉得有必要对参数进行关键字声明时 可以使用 * 符号进行强制关键字传参
# 如 health_check(name,age,*,height,weight,h)
def health_check1(name, age, *, height, weight, h):
print("您的姓名是:{},年龄:{}".format(name, age))
print("您的身体状况良好")
return "ok"
print(health_check1("张三", 13, height=13, weight=14, h=5))
# 也可以使用 字典进行传参
json1 = {"name": "张三", "age": 13, "address": "中国重庆市南岸区"}
health_check(json1.get("name"), json1.get("age"), 1123, 123)
# 函数的 序列传参 字典传参 返回多个数据的传参
"""
序列传参
"""
import random
def health_check1(name, age, height, weight, h):
print("您的姓名是:{},年龄:{}".format(name, age))
print("您的身体状况良好")
return "ok"
list1 = ["张三", 13, 170, 70, 4]
# 需要注意的是 在列表序列传参是 需要在方法调用时 参数前面增加"*" function(*list) 星号 以代表是序列传参
print(health_check1(*list1))
print("===============================")
"""
字典传参
"""
# 需要注意的是 在字典传参是 需要在方法调用时 参数前面增加 "**" function(**list) 两个星号 以代表是字典传参
dict1 = {"name": "李四", "age": 13, "height": 180, "weight": 75, "h": 123}
print(health_check1(**dict1))
print("===============================")
"""
返回多个数据的方法
"""
# 返回多个数据方法 如果是返回的多个参数 或者多层数据的时候 在这里就可以使用字典传参
# 与 java 类似 返回对象或者HashMap 的形式
def get_dict_info():
result = {
"employee": [
{"name": "张三", "age": 13, "address": "重庆"},
{"name": "李四", "age": 24, "address": "上海"}
],
"device": {
8873434: {"name": "超级游戏笔记本", "price": "12333$"},
9934834: {"name": "超级跑车", "price": "2000000$"}
},
"city": {},
"student": {}
}
return result
json = get_dict_info()
print(json.get("employee")[0].get("name"))
"""
个人练习 生活小助理
"""
# 匪警[110],火警[119],急救中心[120],道路交通事故报警[122],水上求救专用电话[12395],天气预报[12121],报时服务[12117],森林火警[12119],电力服务[95598],红十字会急救台[999],公安短信报警[12110],通用紧急求救[112],信产部IP/网站备案[010-66411166]
# 北京,2019年1月12日,多云,8°C,-4°C,南风3级~上海,2019年1月12日,小雨,9°C,6°C,北风2级~广州,2019年1月12日,阵雨转多云,22°C,15°C,持续无风向微风
import random
# 随机生成指定注数双色球
def random_num(number):
for a in range(0, number):
for i in range(0, 6):
print(random.randint(1, 33), end=" ")
print(random.randint(1, 16))
# 查询机构电话号码
def query_phone_number(query_str):
phone_list = phone.split(",")
for p in phone_list:
if query_str in p:
print(p)
# 查询天气方法
def query_weather(query_city):
city_list = citys.split("~")
dict1 = {}
for i in range(0, len(city_list)):
temp_city = city_list[i].split(",")
dict1[temp_city[0]] = {"city": temp_city[0], "date": temp_city[1], "a": temp_city[2], "b": temp_city[3],
"c": temp_city[4], "d": temp_city[5]}
query_city = dict1.get(query_city)
if query_city is not None:
print("{city}市{date}天气{a}最高温度{b}最低问题{c}风向{d}".format_map(query_city))
else:
print("未查询到天气情况")
phone = "匪警[110],火警[119],急救中心[120],道路交通事故报警[122],水上求救专用电话[12395],天气预报[12121],报时服务[12117],森林火警[12119],电力服务[95598],红十字会急救台[999],公安短信报警[12110],通用紧急求救[112],信产部IP/网站备案[010-66411166]"
citys = "北京,2019年1月12日,多云,8°C,-4°C,南风3级~上海,2019年1月12日,小雨,9°C,6°C,北风2级~广州,2019年1月12日,阵雨转多云,22°C,15°C,持续无风向微风"
print("欢迎访问生活小助手")
while True:
print("1-双色球随机选号")
print("2-号码百事通")
print("3-明日天气预报")
print("0-结束程序")
n = int(input("请输入功能编号:"))
if n == 1:
num = int(input("您要生成几注双色球号码:"))
random_num(num)
elif n == 2:
query = input("请输入您要查询的机构或者电话号码:")
query_phone_number(query_str=query)
elif n == 3:
city = input("请输入您要查询的城市:")
query_weather(query_city=city)
elif n == 0:
print("欢迎使用生活助手,再见!")
break
else:
print("输入错误,请重新输入")
print("=" * 50)
Python-function函数的基本使用
最新推荐文章于 2022-05-05 17:15:01 发布