python第八章作业
实例一:
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个元素的列表,
分别为姓名、身高(单位:米)和体重(单位:千克)
'''
实例二:
(1):
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))
(2):
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))
(3):
import rectangle as r
import circular as c
if __name__=='__main__':
print("圆形的周长为:",c.girth(10))
print("矩形的周长为:",r.girth(10,20))

实例三:
_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())

实例四:
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
n=int(input("请输入要生成的大乐透号码注数:"))
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 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恭喜您集成五福!!!')

实战三:
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)

实战四:
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)

浙公网安备 33010602011771号