"" "Title: the bonus paid by the enterprise is based on the profit commission. Profit (I) less than or equal to $100,000, the bonus can be raised by 10%, the profit is higher than $100,000, less than $200,000, the portion of less than 100,000 yuan by 10% commission, higher than the portion of 100,000 yuan, a commission of 7.5%, 200,000 to 400,000, higher than 200,000 yuan, can commission 5% ; Between 400,000 and 600,000 is higher than the portion of 400,000 yuan, can commission 3%, 600,000 to 1 million, higher than 600,000 yuan portion, can commission 1.5%, higher than 1 million yuan, the portion of more than 1 million yuan by 1% Commission, from the keyboard input month profit I, the total bonus should be issued? "" "Import redef calculate1 (Profit):" "" own solution, directly using the simplest idea "" "Print (" bonus calculation One result ", end=": ") if profit < 0:print ("Profit cannot be less than 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-2 00000) * 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"% (pro Fit, (profit-1000000) * 0.01 + 10000 + 7500 + 100 (+ 6000 + 6000)) def calculate2 (profit): "" "" "" to calculate the total bonus "" "Print (" result of the bonus Calculation II ", end=" If profit < 0:print ("profit cannot be less than 0") return Profitrank = (1000000, 600000, 400000, 200000, 100000, 0) p Rofitrate = (0.01, 0.015, 0.03, 0.05, 0.075, 0.1) Tempprofit = Profit Sumbonus = 0 for i in range (0, 6): I F 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): "", calculated using the Sum method, the total amount, It also uses the Zip,map,filter,join function "" "Print (" Bonus calculation three results ", end=": ") if profit < 0:print (" profit cannot be less than 0 ") ret Urn 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, Prof itrate) bonuslist = map (lambda p:p[0] * p[1], pList) bonuslist = List (filter (lambda f:f > 0, bonuslist)) Bon US = SUM (bonuslist) bonusstrlist = map (lambda f: "{:. 2f}". Format (f), bonuslist) print ("%s=%.2f"% ("+". Join (Bonusstr List), bonus) def CALCULATE4 (profit): "" "uses the slice of the list to implement the calculation:p Aram Profit:: return:" "Print (" Bonus calculation four results ", end = ":") if profit <0:print ("Profit cannot be less than 0") return Profitrank = [1000000, 600000, 400000, 200000, 100000, 0] profitrate = [0.0 1, 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:] pr Ofitcalc.insert (0, Profit-profitrank[i]) Break pList = Zip (Profitcalc, profitrate) bonuslist = map (lamb Da P:p[0] * p[1], pList) bonuslist = List (filter (lambda f:f > 0, bonuslist)) bonus = SUM (bonuslist) bonusstr List = map (lambda f: "%.2f"% F, bonuslist) print ("%s=%.2f"% ("+". Join (Bonusstrlist), bonus)) def calculate5 (profit): "" "Using nested lists, in fact, is similar to Calculate2, except to combine two lists into a nested list:p Aram Profit:: return:" "Print (" result of bonus calculation five ", end=": ") If profit < 0:print ("profit cannot be less than 0") return profitrank = [[1000000, 0.01], [600000, 0.015], [400000, 0.0 3], [200000, 0.05], [1000.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 p Rofittemp = Profitrank[i][0] if i = = 5:print ("%.2f"% Bonusrank, end= "=") Else: Print ("%.2f"% Bonusrank, end= "+") print ("%.2f"% bonus) def CALCULATE6 (profit): "" "Calculates bonuses using a dictionary, similar to Calcula TE5, just replace the nested list with a dictionary, the sorting method of the list sort and reverse method reverse:p Aram Profit:: Return: "" "Print (" Bonus calculation six results ", end=": ") if Profit < 0:print ("profit cannot be less than 0") return profitdict = {1 million:0.01, 600,000:0.015, 400,000:0.03, 200000: 0.05, 100000:0.075, 0:0.1} # Dict.keys () returns the thing iterator Profitkey = list (Profitdict.keys ()) Profitkey.sort () profitk Ey.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): "" The bonus is calculated using the recursive return, similar to Calculate1 :p Aram S::p Aram Profit:: Return: "" "if s = = 0:print (" Bonus calculation Seven results ", end=": ") if profit < 0: Print ("Profit cannot be less than 0") return elif 0 <= profit <= 100000:bonus1 = Bonus = Profit * 0.1 elif 10000 0 < profit <= 200000:bonus1 = (profit-100000) * 0.075 bonus = Bonus1 + Calculate7 (100000, 1) Eli F 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 (40000 0, 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 Bonusdef Answer (): "" "Answer the question, enter the profit, and then call the calculation method to calculate the profit but before the calculation, you need to determine the input is a numeric type, because input is the STR type str is the string str.isalnum () all characters are number Word or Letter str.isalpha () All characters are letters STR.ISDIGIT () All characters are numeric str.islower () All characters are lowercase str.isupper () all characters are is uppercase Str.istitle () all the words are capitalized, like the title Str.isspace () all characters are white space characters, \ t, \ n, \ r are mainly for integers, but not for floating-point numbers. Number how to judge, 1. Judging by the exception, try:f = float (str) exception Valueerror:print ("Input is not a number!") ") 2. Judge by regular expression: ' ^[-+]? [0-9]+ (\.[ 0-9]+)? $ "" "Profit = Input (" Enter profit Amount: ") if Profit = =" Q ": Return reg = Re.compile (' ^[-+]?[ 0-9]+ (\.[ 0-9]+)? $ ') Result = Reg.match (profit) If Result:profitfloat = float (profit) calculate1 (profitfloat) calculate2 (pr Ofitfloat) Calculate3 (profitfloat) calculate4 (profitfloat) calculate5 (profitfloat) Calculate6 ( Profitfloat) Calculate7 (profitfloat, 0) else:print ("Error: input is not a numeric value. ") Print (" Continue, or input Q launch ") answer () answer ()
Python Learning-Exercises (2)