1.创建计算BMI指数的模块
def dun_bmi(person,height,weight):
print(person + "的身高" + str(height) + "米\t 体重:" + str(weight) + "千克" )
bmi = weight / (height * height)
print(person + "的bmi指数为: " + str(bmi))
2.导入两个包括同名函数的模块
def girth(width,height):
return (width + height) * 2
def area(width,heigth):
return width * heigth
if __name__ == '__main__':
print(area(10,20))
import math
PI = math.pi
def girth(r):
return round(2 * PI * r , 2)
def area(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))
![](/i/l/?n=22&i=blog/2976031/202211/2976031-20221115164610670-1597894926.png)
3.在指定包中创建通用的设置和获取尺寸的模块
_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())
![](/i/l/?n=22&i=blog/2976031/202211/2976031-20221115170054524-454199943.png)
4.生成由数字,字母组成的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)
实战一:
import random
def number():
a=[random.randint(1,35) for i in range(5)]
l=[]
for i in a:
if i not in l:
l.append(i)
else:
i=random.randint(1,35)
l.append(i)
l.sort()
a=[str(i) for i in l]
b=[random.randint(1,12) for i in range(2)]
o=[]
for i in b:
if i not in o:
o.append(i)
else:
i=random.randint(1,12)
o.append(i)
o.sort()
b=[str(i) for i in o]
c=[" "*5]
for i in range(5):
if len(a[i])==2:
for j in range(2):
if len(b[j])==2:
pass
else:
b[j]='0'+b[j]
else:
a[i]='0'+a[i]
g=a+c+b
for i in g:
print(i,end=" ")
print("大乐透号码生成器")
k=int(input("请输入要生成的大乐透号码注数:"))
for i in range(k):
number()
print("")
![](/i/l/?n=22&i=blog/2976031/202211/2976031-20221115170923734-6873724.png)
实战二:
import sys
sys.path.append(r"D:\Python\python0\python0")
import JiFu
print('开始集福啦~~~')
fu={'爱国福':0,'富强福':0,'和谐福':0,'友善福':0,'敬业福':0}
while JiFu.five_blessings(fu)==0:
input('\n按下<Enter>键获取五福')
Strfu=JiFu.Ji_Fu()[0]
print('获取到:' +Strfu)
fu[Strfu] += 1
JiFu.fus(fu)
print('\n恭喜您集成五福!!!')
import random
def Ji_Fu():
fus=['爱国福','富强福','和谐福','友善福','敬业福']
fu=random.sample(fus, 1)
return fu
def fus(fu):
print('当前拥有的福:')
for i, j in fu.items():
print(i,': ',j,'\t',end='')
def five_blessings(fu):
type=1
for i, j in fu.items():
if j==0:
type=0
return type;
![](/i/l/?n=22&i=blog/2976031/202211/2976031-20221115173648704-1744882800.png)
实战3
def net_play(time):
print('浏览网页',str(time)+'小时')
return time
def Watch_videos(time):
print( '看视频',str(time)+'小时')
return time
def Play_game(time):
print('玩网络游戏',str(time)+'小时')
return time
def Study(time):
print( '上网学习',str(time)+'小时')
return time
def times(time):
if time>=8:
print('今天上网时间共计'+str(time)+'小时,请保护眼睛,合理安排上网时间!')
return time
import sys
sys.path.append(r"D:\Python\python0\python0")
import Net
name='小明'
time=0;
print(name,'上网时间、行为统计:')
time += Net.net_play(1.5)
time += Net.Watch_videos( 2)
time += Net.Play_game( 3)
time += Net.Study(2)
Net.times(time)
实战四
import sys
sys.path.append(r"D:\Python\python0\python0")
import tax
monthMoney=int(input("请输入月收入:"))
print("应纳个人所得税税额为%.2f" % tax.tax(monthMoney))
def tax(monthMoney):
ds = 3500
threeInsurancesUp = 7662
yangLao = monthMoney * 0.08
yiLiao = monthMoney * 0.02
shiYe = monthMoney * 0.005
homeMoney = monthMoney * 0.12
threeInsurances = yangLao + yiLiao + shiYe + homeMoney
if threeInsurances > threeInsurancesUp:
threeInsurances = threeInsurancesUp
payable = monthMoney - threeInsurances - ds
single = 0
if payable < 1500:
single = payable * 0.03 - 0
elif payable >= 1500 and payable < 4500:
single = payable * 0.1 - 105
elif payable >= 4500 and payable < 9000:
single = payable * 0.2 - 555
elif payable >= 9000 and payable < 35000:
single = payable * 0.25 - 1005
elif payable >= 35000 and payable < 55000:
single = payable * 0.3 - 2002
elif payable >= 55000 and payable < 80000:
single = payable * 0.35 - 5505
elif payable >= 80000:
single = payable * 0.45 - 13505
if single < 0:
single = 0
return single
![](/i/l/?n=22&i=blog/2976031/202211/2976031-20221115180319201-1030448056.png)