Python_第六章实例及实战+课后作业
一.实验内容:《零基础学Python》第六章实例和实战,以及一道作业题
二.实验环境:visual studio 2022
三.实验目的和要求:掌握定义和调用函数、变量的作用域、匿名函数、参数传递、返回值等
四.实验内容:
- 实例01 输出每日一帖(共享版)
在IDLE中创建一个名称为function_tips.py的文件,然后在该文件中创建一个名称为function_tips的函数,在该函数中,从励志文字列表中获取一条励志文字并输出,最后再调用函数function_tips(),代码如下:
def function_tips(): '''功能:每天输出一条励志文字 ''' import datetime #导入日期时间类 #定义一个列表 mot=["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无选择", "今天星期二:\n含泪播种的人一定能笑着收获", "今天星期三:\n 做对的事情比把事情做对重要", "今天星期四:\n 命运给予我们的不是失望之酒,而是机会之杯", "今天星期五:\n 不要等到明天,明天太遥远,今天就行动", "今天星期六:\n 求知若饥,虚心若愚", "今天星期七:\n 成功属于那些从不说”不可能“的人" ] day=datetime.datetime.now().weekday() #获取当前日期 print(mot[day]) #输出每日一帖 #*******************************调用函数********************************* function_tips()
运行结果:
- 实例02 根据身高、体重计算BMI指数(共享版)
在IDLE中创建一个名称为function_bmi.py的文件,然后在该文件中定义一个名称为fun_bmi的函数,该函数包括3个参数,分别用于指定姓名、身高和体重,在根据公式:BMI=体重/(身高×身高)计算BMI指数,并输出结果,最后在函数体外调用两次fun_bmi函数,代码如下:
def fun_bmi(person,height,weight): '''功能:根据身高和体重计算BMI指数 person:姓名 height:身高,单位:米 weight:体重,单位:千克 ''' print(person+'的身高:'+str(height)+'米\t 体重:'+str(weight)+'千克') bmi=weight/(height*height) print(person+'的BMI指数为:'+str(bmi)) #判断身材是否合理 if bmi<18.5: print("您的体重过轻~@_@~") if bmi>=18.5 and bmi<24.9: print("正常范围,注意保持(-_-)") if bmi>=24.9 and bmi<29.9: print("您的体重过重~@_@~") if bmi>=29.9: print("肥胖^@_@^") #*********************调用函数********************* fun_bmi("路人甲",1.83,60) fun_bmi("路人乙",1.60,50)
运行结果:
- 实例03 根据身高、体重计算BMI指数(共享升级版)
在IDLE中创建一个名称为function_bmi_upgrade.py的文件,然后在该文件中定义一个名称为fun_bmi_upgrade的函数,该函数包括一个可变参数,用于指定包括姓名、身高和体重的测试人信息,在该函数中将根据测试人信息计算BMI指数,并输出结果,最后在函数体外定义一个列表,并且将该列表作为fun_bmi_upgrade()函数的参数调用,代码如下:
def fun_bmi_upgrade(*person): '''功能:根据身高和体重计算BMI指数 *person:可变参数该参数中需要传递带3个元素的列表, 分别为姓名、身高(单位:米)和体重(单位:千克) ''' for list_person in person: for item in list_person: person=item[0] height=item[1] weight=item[2] print("\n"+"="*13,person,"="*13) print('身高:'+str(height)+'米\t 体重:'+str(weight)+'千克') bmi=weight/(height*height) print('BMI指数:'+str(bmi)) #判断身材是否合理 if bmi<18.5: print("您的体重过轻~@_@~") if bmi>=18.5 and bmi<24.9: print("正常范围,注意保持(-_-)") if bmi>=24.9 and bmi<29.9: print("您的体重过重~@_@~") if bmi>=29.9: print("肥胖^@_@^") #*********************调用函数********************* list_w=[('绮梦',1.70,65),('零语',1.78,50),('黛兰',1.72,66)] list_m=[('梓轩',1.80,75),('冷伊一',1.75,70)] fun_bmi_upgrade(list_w,list_m)
运行结果:
- 实例04 模拟结账功能——计算实付金额
def fun_checkout(money): '''功能:计算商品合计金额并进行折扣处理 money:保存商品金额的列表 返回商品的合计金额和折扣后的金额 ''' money_old=sum(money) money_new=money_old if 500<=money_old<1000: money_new='{:.2f}'.format(money_old*0.9) elif 1000<=money_old<=2000: money_new='{:.2f}'.format(money_old*0.8) elif 2000<=money_old<=3000: money_new='{:.2f}'.format(money_old*0.7) elif money_old>=3000: money_new='{:.2f}'.format(money_old*0.6) return money_old,money_new #*******************调用函数********************* print("\n开始结算......\n") list_money=[] while True: inmoney=float(input("输入商品金额(输入0表示输入完毕):")) if int(inmoney)==0: break else: list_money.append(inmoney) money=fun_checkout(list_money) print("合计金额:",money[0],"应付金额:",money[1])
运行结果:
- 实例05 一棵松树的梦
pinetree='我是一棵松树' def fun_christmastree(): '''功能:一个梦 无返回值 ''' pinetree='挂上彩灯、礼物......我变成一棵圣诞树@^.^@\n' print(pinetree) #*******************函数体外********************* print('\n下雪了......\n') print('==========开始做梦......===========\n') fun_christmastree() print('==========梦醒了......=============\n') pinetree='我身上落满雪花,'+pinetree+'-_-' print(pinetree)
运行结果:
- 实例06 应用lambda实现对爬取到的秒杀商品信息进行排序
bookinfo=[('不一样的卡梅拉(全套)',22.50,120),('零基础学Andoid',65.10,89.80), ('摆渡人',23.40,36.00),('福尔摩斯探案全集8册',22.50,128)] print('爬取到的商品信息:\n',bookinfo,'\n') bookinfo.sort(key=lambda x:(x[1],x[1]/x[2])) print('排序后的商品信息:\n',bookinfo)
运行结果:
- 实战01 导演为剧本选主角
代码如下:
def director_lead(*zj ): '''功能:输出确定参演剧本主角的名字 ''' for item in zj: print(item) zj=input('导演选定的主角是:') print(str(zj)+"开始参演这个剧本")
运行结果:
- 实战02 模拟美团外卖商家的套餐
代码如下:
def Mtpackage(*packagename): print('\n米线店套餐如下:1.考神套餐 2.单人套餐 3.情侣套餐') for item in packagename: print(item) param=['考神套餐13元','单人套餐9.9元','情侣套餐20元'] Mtpackage(*param)
运行结果:
- 实战03 根据生日判断星座
代码如下:
def birthday_constellation(month,date): '''功能:根据生日可以判断出所属星座 month:储存月份的变量 date:储存日期的变量 list_star:储存星座的列表 ''' list_star=['摩羯座','水瓶座','双鱼座','白羊座','金牛座','双子座', '巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座'] list_month= (20,19,21,20,21,22,23,23,23,24,23,22) if date<list_month[month-1]: return list_star[month-1] else: return list_star[month] month=int(input('请输入月份(例如:5)')) date=int(input('请输入日期(例如:17)')) print(str(month)+'月'+str(date)+'日星座为:',birthday_constellation(month,date))
运行结果:
- 实战04 将美元转换为人民币
代码如下:
def dollar_RMB( my ): '''功能:将美元按照汇率“1美元=6.28人民币”转换为人民币 ''' rmb=my*6.28 return rmb my=int(input('请输入要转换的美元金额:')) print('转换后人民币金额是:',dollar_RMB(my))
运行结果:
- 作业01
编写一段程序,判断输入的电话号码是中国联通、中国电信或中国移动
完整代码:
import re
pattern1=r'(13[4-9]\d{8})$|(14[78]\d{8})$|(15[012789]\d{8})$|(17[28]\d{8})$|(18[23478]\d{8})$|(19[578]\d{8})$'
pattern2=r'(13[3]\d{8})$|(14[9]\d{8})$|(17[37]\d{8})$|(18[019]\d{8})$|(19[0139]\d{8})$'
pattern3=r'(13[0-2]\d{8})$|(14[56]\d{8})$|(16[6]\d{8})$|(17[56]\d{8})$|(18[56]\d{8})$|(19[6]\d{8})$'
mobile=input( )
yd=re.match(pattern1,mobile)
dx=re.match(pattern2,mobile)
lt=re.match(pattern3,mobile)
if yd==None and dx==None and len(mobile)==11:
print(mobile,'是中国联通的手机号')
elif yd==None and lt==None and len(mobile)==11:
print(mobile,'是中国电信的手机号')
elif dx==None and lt==None and len(mobile)==11:
print(mobile,'是中国移动的手机号')
else:
print('请输入11位数的手机号码')
、
运行结果: