1. 程式人生 > 其它 >Chapter 8

Chapter 8

Bmi

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))

Size

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())

Checkcode

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)

Project

#實戰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))