一.實驗內容:《零基礎學Python》第六章實體和實戰,以及一道作業題
二.實驗環境:IDLE Shell 3.9.7
三.實驗目的和要求:掌握定義和呼叫函式、變數的作用域、匿名函式、引數傳遞、回傳值等
四.實驗內容:
- 作業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位數的手機號碼')
運行結果:

- 實體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))
運行結果:


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/515059.html
標籤:其他
上一篇:初識C語言
