#参数可以设置默认值,如果调用参数的时候没有给出改参数的值,则使用默认值进行调用 def calc_exchange_rate(amt,source,target = "USD"): print(target) if source == "CNY" and target == "USD": result = amt /6.7516 return result elif source == "CNY" and target == "EUR": result = amt / 7.7512 return result r = calc_exchange_rate(100,"CNY") print(r) r = calc_exchange_rate(100,"CNY","USD") print(r) r = calc_exchange_rate(100,"CNY","EUR") print(r) def health_check(name,age,height,weight): print("okookok") #这样子调用吗,如果单单看调用的那句,很难知道数据是什么意思 #建议使用形参形式传参(关键字床惨),提高可读性,且参数无需按照顺序 health_check("a",12,170,70) health_check(name="a",age=12,height=170,weight=70)#可以不按顺序 #混合形式传参 #定义函数的时候,可以使用*号使之后所有参数传参时必须使用关键字传参 def health_check1(name,*,age,height,weight): print("okookok") #health_check1(name="a",12,height=170,weight=70)报错 health_check1("a",age=12,height=170,weight=70) #注意,如果出现太多的参数,建议使用字典或者其他数据结果传参
结果 USD 14.811303987203035 USD 14.811303987203035 EUR 12.901228196924347 okookok okookok okookok
#序列: #*加上列表名字 def calc(a,b,c): return (a + b) * c l = [1,5,10] print(calc(*l)) #字典 def health_check1(name,age,height,weight): print("okookok") print(name) #在字典名字前面加入两个* 键对应形参,值对应实参即可 param = {"name" : "a","age" : 12,"height" : 170,"weight" : 70} health_check1(**param) #返回多个数据 def get_detail_info(): dict1 = { "employee" : [{"name" : "aaa","salary" : 9999}, {"name" : "bbb","salary" : 9999} ], "device" : [{"id" : 123456,"title" : "笔记本"}, {"id" : 123654,"title" : "台式机"} ], "asset" :[{},{}], "project" : [{},{}] } return dict1 print(get_detail_info()) d = get_detail_info() sal = d.get("employee")[0].get("salary") print(sal)
结果 60 okookok a {'employee': [{'name': 'aaa', 'salary': 9999}, {'name': 'bbb', 'salary': 9999}],
'device': [{'id': 123456, 'title': '笔记本'}, {'id': 123654, 'title': '台式机'}],
'asset': [{}, {}], 'project': [{}, {}]} 9999
import random #random.randint(1,16) def double_ball(number): for j in range(0,int(number)): for i in range(0,6): red = random.randint(1,33) print(red,end="\t") blue = random.randint(1,16) print(blue) phone_num_str = "匪警[110],火警[119],急救中心[120]" def call_num(callw): phone_list = phone_num_str.split(",") for p in phone_list: if p.find(callw) != -1: print(p) weather_str = "北京,2019,多云,8-14~上海,2019,小雨,20-25" def get_weather(city): weather_list = weather_str.split("~") weather_data = {} for i in range(0, len(weather_list)): w = weather_list[i].split(",") weather = {"name": w[0], "date": w[1], "weather": w[2], "wind": w[3]} # print(weather) weather_data[weather["name"]] = weather if city in weather_data: return weather_data[city] else: return {} while True: print("1-双色球") print("2-号码百事") print("3-天气预报") print("0-退出") inms = input("请输入功能编号:\t") if inms == "1": num = input("多少组:\t") double_ball(num) elif inms == "2": n = input("请输入你要查询的机构或者电话好吗;\t") call_num(n) elif inms == "3": cname = input("你要查询那个城市的天气") # print(get_weather(cname)) w = get_weather(cname) if "name" in w: print("{dat} {name} {weather} {wind}".format_map(w)) else: print("未找到{0}的天气信息".format(cname)) elif inms == "0": print("happy") break else: print("输入错误,请重新输入")e