Python第六周
一. 实验目的和要求
掌握python字符串拼接、截取、分割和合并等操作。
二. 实验环境
python 3.10 64-bit
三. 实验过程
实例1
代码如下:
1 def function_tips(): 2 '''功能:每天输出一条励志文字 3 ''' 4 import datetime #导入日期时间类 5 #定义一个列表 6 mot = ["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无选择", 7 "今天星期二:\n含泪播种的人一定能笑着收获", 8 "今天星期三:\n做对的事情比把事情做对主要", 9 "今天星期四:\n命运给予我们的不是失望之酒,而是机会之杯", 10 "今天星期五:\n不要等到明天,明天太遥远,今天就行动", 11 "今天星期六:\n求知若渴,虚心若愚", 12 "今天星期日:\n成功将属于那些从不说“不可能”的人",] 13 day = datetime.datetime.now().weekday() # 获取当前星期 14 print(mot[day]) # 输出每日一帖 15 #*********************************调用函数*****************************************# 16 function_tips() # 调用函数
运行结果:

实例2
代码如下:
1 def fun_bmi(person,height,weight): 2 '''功能:根据身高和体重计算BMI指数 3 person:姓名 4 height:身高,单位:米 5 weight:体重,单位:千克 6 ''' 7 print(person + "的身高" + str(height) + "米\t 体重:" + str(weight) +"千克") 8 bmi=weight/(height*height) 9 print(person + "的BMI指数为:"+str(bmi)) 10 # 判断身,60材是否合理 11 if bmi<18.5: 12 print("您的体重过轻 ~@_@~\n") 13 if bmi>=18.5 and bmi<24.9: 14 print("正常范围,注意保持 (-_-)\n") 15 if bmi>=24.9 and bmi<29.9: 16 print("您的体重过重 ~@_@~\n") 17 if bmi>=29.9: 18 print("肥胖 ^@_@^\n") 19 #***************************************调用函数********************************# 20 fun_bmi("路人甲",1.83,60) 21 fun_bmi("路人乙",1.60,50) 22
运行结果:


实例3
代码如下:
1 def fun_bmi_upgrade(*person): 2 '''功能:根据身高和体重计算BMI指数(共享升级版) 3 *person:可变参数该参数中需要传递带3个元素的列表, 4 分别为姓名、身高(单位:米)和体重(单位:千克) 5 ''' 6 for list_person in person: 7 for item in list_person: 8 person = item[0] 9 height = item[1] 10 weight = item[2] 11 print("\n" + "="*13,person,"="*13) 12 print("身高:" + str(height) + "米\t 体重:" + str(weight) +"千克") 13 bmi=weight/(height*height) 14 print("BMI指数:"+str(bmi)) 15 # 判断身,60材是否合理 16 if bmi<18.5: 17 print("您的体重过轻 ~@_@~\n") 18 if bmi>=18.5 and bmi<24.9: 19 print("正常范围,注意保持 (-_-)\n") 20 if bmi>=24.9 and bmi<29.9: 21 print("您的体重过重 ~@_@~\n") 22 if bmi>=29.9: 23 print("肥胖 ^@_@^\n") 24 #***************************************调用函数********************************# 25 list_w = [('绮梦',1.70,65),('零语',1.78,50),('黛兰',1.72,66)] 26 list_m = [('梓轩',1.80,75),('冷伊一',1.75,70)] 27 fun_bmi_upgrade(list_w ,list_m) 28
运行结果:

实例4
代码如下:
1 def fun_checkout(money): 2 '''功能:计算商品合计金额并进行折扣处理 3 money:保存商品金额的列表 4 返回商品的合计金额和折扣后的金额 5 ''' 6 money_old = sum(money) 7 money_new = money_old 8 if 500<=money_old<1000: 9 money_new ='{:2f}'.format(money_old*0.9) 10 elif 1000<=money_old<2000: 11 money_new='{:2f}'.format(money_old*0.8) 12 elif 2000<=money_old<3000: 13 money_new='{:2f}'.format(money_old*0.7) 14 elif 2000<=money_old>=3000: 15 money_new='{:2f}'.format(money_old*0.6) 16 return money_old,money_new 17 #**********************调用函数***************************# 18 print("\n开始结算……\n") 19 list_money = [] 20 while True: 21 #请不要输入非法的金额,否则将抛出异常 22 inmoney = float(input("输入商品金额(输入0表示输入完毕):")) 23 if int(inmoney) == 0: 24 break 25 else: 26 list_money.append(inmoney) 27 money = fun_checkout(list_money) 28 print("合计金额:",money[0],"应付金额:",money[1])
运行结果:

实例5
代码如下:
1 pinetree = '我是一棵松树' 2 def fun_christmastree(): 3 '''功能:一个梦 4 无返回值 5 ''' 6 pinetree = '挂上彩灯、礼物……我变成一棵圣诞树\n' 7 print(pinetree) 8 #**********************函数体外********************# 9 print('\n下雪了……\n') 10 print('===============开始做梦…… ===============\n') 11 fun_christmastree() 12 print('===============梦醒了…… =================\n') 13 pinetree = '我身上落满雪花,'+pinetree 14 print(pinetree)
运行结果:

实例6
代码如下:
1 bookinfo = [('不一样的卡梅拉(全套)',22.50,120),('零基础学Android',65.10,89.80), 2 ('摆渡人',23.40,36.00),('福尔摩斯探案全集8册',22.50,128)] 3 print('爬取到的商品信息:\n',bookinfo,'\n') 4 bookinfo.sort(key = lambda x:(x[1],x[1]/x[2])) 5 print('排序后的商品信息:\n',bookinfo)
运行结果:

实战1:
代码如下:
1 def Actor(actor): 2 print(actor + "开始参演这个剧本") 3 4 #调用函数 5 person = input("导演选定的主角是:") 6 Actor(person)
运行结果:

实战2:
代码如下:
1 def Package(package1, money1, package2, money2, package3, money3): 2 print('米线店套餐如下:1.'+ package1 + '2.' + package2 + '3.' +package3) 3 print(package1 + money1) 4 print(package2 + money2) 5 print(package3 + money3) 6 7 #调用函数 8 Package('考神套餐', '13元', '单人套餐', '9.9元', '情侣套餐', '20元')
运行结果:

实战3:
代码如下:
1 list = ['摩羯座','水瓶座','双鱼座','白羊座','金牛座','双子座', 2 '巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座'] 3 d = [20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22] 4 def starSign(month, day): 5 if day < d[month-1]: 6 return list[month-1] 7 else: 8 return list[month] 9 10 #调用函数 11 month = int(input("请输入月份:")) 12 day = int(input("请输入日期:")) 13 print(str(month) + "月" + str(day) + "日星座为:" + starSign(month, day))
运行结果:

实战4:
代码如下:
1 def change(dollar): 2 rmb = dollar * 6.28 3 return rmb 4 5 #调用函数 6 dollar = float(input("请输入要转换的美元金额:")) 7 rmb = change(dollar) 8 print("转换后人民币金额是:" + str(rmb))
运行结果:

实践1:
代码如下:
1 import re 2 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})$' 3 pattern2 = r'(133\d{8})$|(149\d{8})$|(153\d{8})$|(17[37]\d{8})$|(18[013]\d{8})$|(19[0139]\d{8})$' 4 pattern3 = r'(13[012]\d{8})$|(14[56]\d{8})$|(15[56]\d{8})$|(166\d{8})$|(17[56]\d{8})$|(18[56]\d{8})$|(196\d{8})$' 5 mobile = input("请输入11位电话号码:") 6 match = re.match(pattern1,mobile) 7 if match == None: 8 print(mobile,'不是移动号码') 9 else: 10 print(mobile,'是移动号码') 11 match = re.match(pattern2,mobile) 12 if match == None: 13 print(mobile,'不是联通号码') 14 else: 15 print(mobile,'是联通号码') 16 match = re.match(pattern3,mobile) 17 if match == None: 18 print(mobile,'不是电信号码') 19 else: 20 print(mobile,'是电信号码')
运行结果:

浙公网安备 33010602011771号