08代码
实例1
bmi.py
def fun_bmi(person,height,weight):
print(person+'的身高'+str(height)+'米\t 体重:'+str(weight)+'千克')
bmi=weight/(height*height)
print(person+'的BMI指数为:'+str(bmi))
bmi_.py
import bmi
bmi.fun_bmi('jm',175,100)
实例2
rectangle.py
def girth(width,height):
return (width+ height)*2
def area(width,height):
return width*height
if __name__=='__main__':
print(area(10,20))
circular.py
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))
compute.py
import rectangle as r
import circular as c
if __name__=='__main__':
print('圆形的周长为:',c.girth(10))
print('矩形的周长为:',r.girth(10,20))
实例3
Settings.size.py
__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
main.py
from settings.size import *
if __name__=='__main__':
change(1024,768)
print('宽度:',getWidth())
print('高度:',getHeight())
实例4
checkcode.py
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)
实战1
import random
def number():
numbers=[]
leftlist=list(range(1,36))
leftnumber=random.sample(leftlist,5)
rightlist=list(range(1,13))
rightnumber=random.sample(rightlist,2)
leftnumber.sort()
rightnumber.sort()
numbers=leftnumber+rightnumber
template='{:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d} {:0>2d}'
numbers=template.format(numbers[0],numbers[1],numbers[2],numbers[3],numbers[4],numbers[5],numbers[6])
print(numbers)
print('大乐透号码生成器')
while True:
a=int(input('请输入要生成的大乐透号码注数:'))
for i in range(a):
number()
print('')
break
实战2
import random
five_blessings=['爱国福','富强福','和谐福','友善福','敬业福']
print('开始集福啦~~~')
def collect_blessings():
a=0
f=0
h=0
y=0
j=0
while True:
input('按下<enter>键获取五福')
choice=random.choice(five_blessings)
print('获取到'+choice)
print('当前拥有的福:')
if choice=='爱国福':
a+=1
print('爱国福:\t'+str(a)+'富强福:\t'+str(f)+'和谐福:\t'+str(h)+'友善福:\t'+str(y)+'敬业福:\t'+str(j))
elif choice=='富强福':
f+=1
print('爱国福:\t'+str(a)+'富强福:\t'+str(f)+'和谐福:\t'+str(h)+'友善福:\t'+str(y)+'敬业福:\t'+str(j))
elif choice=='和谐福':
h+=1
print('爱国福:\t'+str(a)+'富强福:\t'+str(f)+'和谐福:\t'+str(h)+'友善福:\t'+str(y)+'敬业福:\t'+str(j))
elif choice=='友善福':
y+=1
print('爱国福:\t'+str(a)+'富强福:\t'+str(f)+'和谐福:\t'+str(h)+'友善福:\t'+str(y)+'敬业福:\t'+str(j))
else:
j+=1
print('爱国福:\t'+str(a)+'富强福:\t'+str(f)+'和谐福:\t'+str(h)+'友善福:\t'+str(y)+'敬业福:\t'+str(j))
collect_blessings()
实战3
Net.py
def web(time):
print('浏览网页:'+str(time)+'小时;')
return time
def video(time):
print('看视频:'+str(time)+'小时;')
return time
def playgame(time):
print('玩网络游戏:'+str(time)+'小时;')
return time
def study(time):
print('上网学习:'+str(time)+'小时;')
return time
def total(time):
print('今天上网时间共计:'+str(time)+'小时;',end=' ')
if time>=8:
print('请保护眼睛,合理安排上网时间')
else:
print('比较合理,注意休息')
Net_1
import Net
name='小明'
print(name,'上网时间,行为统计')
t1=Net.web(1.5)
t2=Net.video(2)
t3=Net.playgame(3)
t4=Net.study(2)
time=t1+t2+t3+t4
Net.total(time)
实战4
Tax.py
def tax(monthmoney):
ds=3500
baoxian=7662
yanglao=monthmoney*0.08
yiliao=monthmoney*0.02
shiye=monthmoney*0.005
housem=monthmoney*0.12
demoney=yanglao+yiliao+shiye+housem
if demoney>=7662:
demoney>=7662
lastmoney=monthmoney-demoney-ds
if lastmoney<=0:
taxes=0
elif 0<lastmoney<1500:
taxes=lastmoney*0.03
elif 1500<=lastmoney<4500:
taxes=lastmoney*0.1-105
elif 4500<=lastmoney<9000:
taxes=lastmoney*0.2-555
elif 9000<=lastmoney<35000:
taxes=lastmoney*0.25-1005
elif 35000<=lastmoney<55000:
taxes=lastmoney*0.3-2002
elif 55000<=lastmoney<80000:
taxes=lastmoney*0.35-5505
elif 80000<=lastmoney:
taxes=lastmoney*0.45-13505
return taxes
taxes.py
import tax
m=float(input('请输入您的月收入总额:'))
taxes=tax.tax(m)
print('您应收个人所得税金额为:{:.2f}元'.format(taxes))