1. 程式人生 > >python學習——練習題(2)

python學習——練習題(2)

tle 浮點數 參考 port space 100萬 直接 方法 但是

"""
    題目:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;
    利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;
    20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;
    60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,
    從鍵盤輸入當月利潤I,求應發放獎金總數?
"""
import re


def calculate1(profit):
    """
        自己的解答,直接使用最簡單的思路
    """
    print("獎金計算一的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
    elif 0 <= profit <= 100000:
        print("%.2f*0.1=%.2f" % (profit, profit * 0.1))
    elif 100000 < profit <= 200000:
        print("(%.2f-100000)*0.075+10000=%.2f" % (profit, (profit - 100000) * 0.075 + 10000))
    elif 200000 < profit <= 400000:
        print("(%.2f-200000)*0.05+10000+7500=%.2f" % (profit, (profit - 200000) * 0.05 + 10000 + 7500))
    elif 400000 < profit <= 600000:
        print("((%.2f-400000)*0.03+10000+7500+10000=%.2f" % (profit, (profit - 400000) * 0.03 + 10000 + 7500 + 10000))
    elif 600000 < profit <= 1000000:
        print("((%.2f-600000)*0.015+10000+7500+10000+6000=%.2f" % (profit, (profit - 600000) * 0.015 + 10000 + 7500 +
                                                                   10000 + 6000))
    else:
        print("((%.2f-1000000)*0.01+10000+7500+10000+6000+6000=%.2f" % (profit, (profit - 1000000) * 0.01 + 10000 + 7500
                                                                        + 10000 + 6000 + 6000))


def calculate2(profit):
    """
        參考答案,通過循環已排序好的階段值列表,來計算總獎金
    """
    print("獎金計算二的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    profitRank = (1000000, 600000, 400000, 200000, 100000, 0)
    profitRate = (0.01, 0.015, 0.03, 0.05, 0.075, 0.1)
    tempProfit = profit
    sumBonus = 0
    for i in range(0, 6):
        if tempProfit > profitRank[i]:
            rankBonus = (tempProfit - profitRank[i]) * profitRate[i]
            sumBonus += rankBonus
            tempProfit = profitRank[i]
            if i == 5:
                print("%.2f" % rankBonus, end="=")
            else:
                print("%.2f" % rankBonus, end="+")
    print("%.2f" % sumBonus)


def calculate3(profit):
    """
        利用sum方法計算,總金額, 其中還用到了zip,map,filter,join等函數
    """
    print("獎金計算三的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    profitRank = [1000000, 600000, 400000, 200000, 100000, 0]
    profitRate = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
    profitCalc = [0, 0, 0, 0, 0, 0]
    tempProfit = profit
    for i in range(0, 6):
        if tempProfit > profitRank[i]:
            profitCalc[i] = tempProfit - profitRank[i]
            tempProfit = profitRank[i]
    pList = zip(profitCalc, profitRate)
    bonusList = map(lambda p: p[0] * p[1], pList)
    bonusList = list(filter(lambda f: f > 0, bonusList))
    bonus = sum(bonusList)
    bonusStrList = map(lambda f: "{:.2f}".format(f), bonusList)
    print("%s=%.2f" % ("+".join(bonusStrList), bonus))


def calculate4(profit):
    """
    利用列表的切片來實現計算
    :param profit:
    :return:
    """
    print("獎金計算四的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    profitRank = [1000000, 600000, 400000, 200000, 100000, 0]
    profitRate = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
    profitCalc = [400000, 200000, 200000, 100000, 100000]
    for i in range(0, 6):
        if profit > profitRank[i]:
            profitRate = profitRate[i:]
            profitCalc = profitCalc[i:]
            profitCalc.insert(0, profit - profitRank[i])
            break
    pList = zip(profitCalc, profitRate)
    bonusList = map(lambda p: p[0] * p[1], pList)
    bonusList = list(filter(lambda f: f > 0, bonusList))
    bonus = sum(bonusList)
    bonusStrList = map(lambda f: "%.2f" % f, bonusList)
    print("%s=%.2f" % ("+".join(bonusStrList), bonus))


def calculate5(profit):
    """
    利用嵌套列表實現,其實和calculate2差不多,只不過是把兩個列表組合到一個嵌套列表中
    :param profit:
    :return:
    """
    print("獎金計算五的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    profitRank = [[1000000, 0.01], [600000, 0.015], [400000, 0.03], [200000, 0.05], [100000, 0.075], [0, 0.1]]
    profitTemp = profit
    bonus = 0
    for i in range(0, 6):
        if profitTemp > profitRank[i][0]:
            bonusRank = (profitTemp - profitRank[i][0]) * profitRank[i][1]
            bonus += bonusRank
            profitTemp = profitRank[i][0]
            if i == 5:
                print("%.2f" % bonusRank, end="=")
            else:
                print("%.2f" % bonusRank, end="+")
    print("%.2f" % bonus)


def calculate6(profit):
    """
    利用字典計算獎金,類似calculate5,只是將嵌套列表換成字典
    中間用到了,列表的排序方法sort和倒序方法reverse
    :param profit:
    :return:
    """
    print("獎金計算六的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    profitDict = {1000000: 0.01, 600000: 0.015, 400000: 0.03, 200000: 0.05, 100000: 0.075, 0: 0.1}
    # dict.keys()返回的事叠代器
    profitKey = list(profitDict.keys())
    profitKey.sort()
    profitKey.reverse()
    profitTemp = profit
    bonus = 0
    for i in range(0, len(profitKey)):
        if profitTemp > profitKey[i]:
            bonusRank = (profitTemp - profitKey[i]) * profitDict[profitKey[i]]
            bonus += bonusRank
            profitTemp = profitKey[i]
            if i == len(profitKey) - 1:
                print("%.2f" % bonusRank, end="=")
            else:
                print("%.2f" % bonusRank, end="+")
    print("%.2f" % bonus)


def calculate7(profit, s):
    """
    利用遞歸來計算獎金,類似calculate1
    :param s:
    :param profit:
    :return:
    """
    if s == 0:
        print("獎金計算七的結果", end=":")
    if profit < 0:
        print("利潤不能小於0")
        return
    elif 0 <= profit <= 100000:
        bonus1 = bonus = profit * 0.1
    elif 100000 < profit <= 200000:
        bonus1 = (profit - 100000) * 0.075
        bonus = bonus1 + calculate7(100000, 1)
    elif 200000 < profit <= 400000:
        bonus1 = (profit - 200000) * 0.05
        bonus = bonus1 + calculate7(200000, 1)
    elif 400000 < profit <= 600000:
        bonus1 = (profit - 400000) * 0.03
        bonus = bonus1 + calculate7(400000, 1)
    elif 600000 < profit <= 1000000:
        bonus1 = (profit - 600000) * 0.015
        bonus = bonus1 + calculate7(600000, 1)
    else:
        bonus1 = (profit - 1000000) * 0.01
        bonus = bonus1 + calculate7(1000000, 1)
    if s == 0:
        print("%.2f" % bonus1, end="=")
        print("%.2f" % bonus)
    else:
        print("%.2f" % bonus1, end="+")
    return bonus


def answer():
    """
        解題答案,輸入利潤,然後調用計算方法計算利潤
        不過在計算前,需要判斷一下輸入的是數值類型的,因為input輸入的都是str類型

        str為字符串 str.isalnum() 所有字符都是數字或者字母
        str.isalpha() 所有字符都是字母
        str.isdigit() 所有字符都是數字
        str.islower() 所有字符都是小寫
        str.isupper() 所有字符都是大寫
        str.istitle() 所有單詞都是首字母大寫,像標題
        str.isspace() 所有字符都是空白字符、\t、\n、\r
        上述的主要是針對整型的數字,但是對於浮點數來說就不適用了,那麽浮點數怎麽判斷呢,
        1.通過異常來判斷,try:     f = float(str) exception ValueError:     print("輸入的不是數字!")
        2.通過正則表達式來判斷:‘^[-+]?[0-9]+(\.[0-9]+)?$
    """
    profit = input("輸入利潤金額:")
    if profit == "q":
        return
    reg = re.compile(‘^[-+]?[0-9]+(\.[0-9]+)?$‘)
    result = reg.match(profit)
    if result:
        profitFloat = float(profit)
        calculate1(profitFloat)
        calculate2(profitFloat)
        calculate3(profitFloat)
        calculate4(profitFloat)
        calculate5(profitFloat)
        calculate6(profitFloat)
        calculate7(profitFloat, 0)
    else:
        print("Error:輸入的不是數值。")
    print("繼續,或輸入q推出")
    answer()


answer()

  

python學習——練習題(2)