一.實驗物件:《零基礎學Python》第八章的3道實體和4道實戰
二.實驗環境:IDLE Shell 3.9.7
三.實驗要求:學習使用標準模塊和第三方模塊
四.實驗程序:
- 實體01 創建計算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))
#此處省略了顯示判斷結果的代碼
def fun_bmi_upgrade(*person):
'''功能:根據身高和體重計算BMI指數(升級版)
*person:可變引數該引數中需要傳遞帶3個元素的串列,
分別為姓名、身高(單位:米)和體重(單位:千克)
'''
#此處省略了函式主體代碼
- 實體02 匯入兩個包括同名函式的模塊

點擊查看代碼
def girth(width,height):
'''功能:計算周長
引數:width(寬度)、height(高)
'''
return(width+height)*2
def area(width,height):
'''功能:計算面積
引數:width(寬度)、height(高)
'''
return width*height
if __name__=='__main__':
print(area(10,20))

點擊查看代碼
import math
PI=math.pi
def girth(r):
'''功能:計算周長
引數:r(半徑)
'''
return round(2*PI*r,2)
def area(r):
'''功能:計算面積
引數:r(半徑)
'''
return round(PI*r*r,2)
if __name__=='__main__':
print(girth(10))

點擊查看代碼
import rectangle as r
import circular as c
if __name__=='__main__':
print("圓形的周長為:",c.girth(10))
print("矩形的周長為:",r.girth(10,20))
運行結果:

- 實體03 在指定包中創建通用的設定和獲取尺寸的模塊

點擊查看代碼
_width=800
_height=600
def change(w,h):
global _width
_width=w
global _height
_height=h
def getWidth():
global _width
return _width
def getHeight():
global _height
return _height

點擊查看代碼
from settings.size import *
if __name__=='__main__':
change(1024,768)
print('寬度:',getWidth())
print('高度:',getHeight())
運行結果:

- 實體04 生成由數字、字母組成的4位驗證碼

點擊查看代碼
import random
if __name__=='__main__':
checkcode=""
for i in range(4):
index=random.randrange(0,4)
if index!=i and index+1!=i:
checkcode+=chr(random.randint(97,122))
elif index+1==i:
checkcode+=chr(random.randint(65,90))
else:
checkcode+=str(random.randint(1,9))
print("驗證碼:",checkcode)
運行結果:

- 實戰一:大樂透號碼生成器

點擊查看代碼
print("大樂透號碼生成器")
import random
number=input("請輸入要生成的大樂透號碼注數:")
n=int(number)
for i in range(n):
QQ=random.sample(range(1, 36), 5)
HQ=random.sample(range(1,13),2)
print('{:0>2d}'.format(QQ[0]),'{:0>2d}'.format(QQ[1]),'{:0>2d}'.format(QQ[2]),'{:0>2d}'.format(QQ[3]),'{:0>2d}'.format(QQ[4]),"\t",'{:0>2d}'.format( HQ[0]),'{:0>2d}'.format(HQ[1]))
運行結果:

- 實戰二:春節集五福

點擊查看代碼
import sys
sys.path.append(r"C:\Python\Python39\Lib")
import random
def Ji_Fu():
wf=['愛國福','富強福','和諧福','友善福','敬業福']
fu=random.sample(wf, 1)
return fu
def wf(fu):
print('當前擁有的福:')
for i, j in fu.items():
print(i,': ',j,'\t',end='')
def WuFu(fu):
type=1
for i, j in fu.items():
if j==0:
type=0
return type;
print('開始集福啦~~~')
wufu={'愛國福':0,'富強福':0,'和諧福':0,'友善福':0,'敬業福':0}
while WuFu(wufu)==0:
input('\n按下<Enter>鍵獲取五福')
Strfu=Ji_Fu()[0]
print('獲取到:' +Strfu)
wufu[Strfu] += 1
wf(wufu)
print('\n恭喜您集成五福!!!')
運行結果:

- 實戰三:封裝用戶的上網行為

點擊查看代碼
import sys
sys.path.append(r"C:\Python\Python39\Lib")
def sw(time):
print('瀏覽網頁',str(time)+'小時')
return time
def ksp(time):
print('看視頻',str(time)+'小時')
return time
def wwlyx(time):
print('玩網路游戲',str(time)+'小時')
return time
def swxx(time):
print('上網學習',str(time)+'小時')
return time
def Time(time):
if time>8:
print("今天上網時間共計"+str(time)+"小時,請保護眼睛,合理安排上網時間!")
return time
import random
person='小明'
time=0
print(person,'上網時間、行為統計:')
time+=sw(1.5)
time+=ksp(2)
time+=wwlyx(3)
time+=swxx(2)
Time(time)
運行結果:

- 實戰四:計算個人所得稅

點擊查看代碼
import sys
sys.path.append(r"C:\Python\Python39\Lib")
def individual_income_tax(monthly_income):
'''由月收入計算個人所得稅'''
Monthly_income=input("請輸入月收入:")
Monthly_Income=int(Monthly_income)
Tax=Monthly_Income*(165/8000)
print('應納個人所得稅稅額為{:.2f}'.format(Tax))
individual_income_tax(8000)
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/536864.html
標籤:其他
上一篇:作業匯報
