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

Chapter 6

Function_tips

def function_tips():
    import datetime
    mot=["今天星期一:\n堅持下去不是因為我很堅強,而是因為我別無選擇。",
         "今天星期二:\n含淚播種的人一定能笑著收穫。",
         "今天星期三:\n作對的事情比把事情做對更重要。",
         "今天星期四:\n命運給予我們的不是失望之酒,而是機會之杯",
         "今天星期五:\n不要等到明天,明天太遙遠,今天就行動",
         "今天星期六:\n求知若飢,虛心若愚",
         "今天星期天:\n成功將屬於那些從不說“不可能”的人。"]
    day=datetime.datetime.now().weekday()
    print(mot[day])
function_tips()

Function_bmi

def fun_bmi(name,height,weight):
   print(name+'的身高:'+str(height)+'米\t體重:'+str(weight)+'千克')
   bmi=weight/(height*height)
   print(name+'的BMI指數為:'+str(bmi))
   #判斷身材是否合理
   if bmi<18.5:
       print("你的體重過輕 ~@_@~")
   if bmi>=18.5 and bmi<24.9:
       print("正常範圍,注意保持(-_-)")
   if bmi>=24.9 and bmi<29.9:
       print("你的體重過重 ~@_@~")
   if bmi>=29.9:
       print("肥胖 ^@_@^")
fun_bmi('路人甲',1.83,60)
fun_bmi('路人乙',1.60,50)

Function_bmi_upgrade

def fun_bmi_upgrade(*person):
    for list_person in person:
        for item in list_person:
            person=item[0]
            height=item[1]
            weight=item[2]
            print('\n'+'='*13,person,'='*13)
            print('身高:'+str(height)+'米\t體重:'+str(weight)+'千克')
            bmi=weight/(height*height)
            print('BMI指數為:'+str(bmi))
            #判斷身材是否合理
            if bmi<18.5:
                print("你的體重過輕 ~@_@~")
            if bmi>=18.5 and bmi<24.9:
                print("正常範圍,注意保持(-_-)")
            if bmi>=24.9 and bmi<29.9:
                print("你的體重過重 ~@_@~")
            if bmi>=29.9:
                print("肥胖 ^@_@^")
list_w=[('crz',1.70,65),('pxy',1.78,50),('gyl',1.72,66)]
list_m=[('gzz',1.80,45),('bgy',1.67,45)]
fun_bmi_upgrade(list_w,list_m)

Checkout

def fun_checkout(money):
    money_old=sum(money)
    money_new=money_old
    if 500<=money_old<1000:
        money_new='{:.2f}'.format(money_old*0.9)
    elif 1000<=money_old<=2000:
        money_new='{:.2f}'.format(money_old*0.8)
    elif 2000<=money_old<=3000:
        money_new='{:.2f}'.format(money_old*0.7)
    elif money_old>=3000:
        money_new='{:.2f}'.format(money_old*0.6)
    return money_old,money_new
print('\n開始結算\n')
list_money=[]
while True:
    inmoney=float(input('輸入商品金額(輸入0表示輸入完畢)'))
    if int(inmoney)==0:
        break
    else:
        list_money.append(inmoney)
money=fun_checkout(list_money)
print('合計金額:',money[0],'應付金額:',money[1])

Differenttree

pinetree='我是一顆松樹'
def fun_christmastree():
    pinetree='掛上彩燈,禮物....我變成一顆聖誕樹@^.^@\n'
    print(pinetree)
print('\n下雪了....\n')
print('-----------開始做夢----------\n')
fun_christmastree()
print('==========夢醒了======\n')
pinetree='我身上落滿雪花,'+pinetree+'-_-'
print(pinetree)

Seckillsort

bookinfo=[('不一樣的卡梅拉(全套)',22.50,120),('零基礎學Android',65.10,89.80),('擺渡人',23.40,36.00),('福爾摩斯探案集',22.50,120)]
print('爬取到的商品資訊:\n',bookinfo,'\n')
bookinfo.sort(key=lambda x:(x[1],x[1]/x[2]))
print('排序後的商品資訊:\n',bookinfo)

Project

#實戰1
def act(actor):
    print(actor+'開始參演這個劇本')
A=input('導演選定的主角是:')
act(A)
#實戰2
def taocan(a,b,c,d,e,f):
    print('米線店套餐如下:1.'+a+'2.'+c+'3.'+e)
    print(a+b)
    print(c+d)
    print(e+f)
taocan('考神套餐','13元','單人套餐','9.9元','情侶套餐','20元')
#實戰3
xz=('摩羯座','水瓶座','雙魚座','白羊座','金牛座','雙子座','巨蟹座','獅子座','處女座','天秤座','射手座','摩羯座')
d=(20,19,21,20,21,22,23,23,23,24,23,22)
def xingzuo(month,day):
    if day<d[month-1]:
        return xz[month-1]
    else:
        return xz[month]
month=int(input('請輸入月份(例如5):'))
day=int(input('請輸入日期(例如17):'))
print(str(month)+'月'+str(day)+'日星座為:',xingzuo(month,day))
#實戰4
def zhuanhuan(a):
    rmb=a*6.28
    return rmb
a=float(input('請輸入要轉換的美元金額:'))
print('轉換後人民幣金額是:',zhuanhuan(a))