"python exercise 002" the bonuses paid by the enterprise are based on the profit percentage. 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, less than 100,000 of the portion of the 10% commission, higher than the portion of 100,000 yuan, Cocoa Commission 7.5%, 200,000 to 400,000, higher than 200,000 yuan of the portion, 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?
Well, the slag like me, can think of is to honestly write If...elif...elif ... Loop function, I'm tired of watching myself ...
i = float (input ("Enter the current month's profit, unit is million:")) if I <=: BNS = ten * 0.1elif < I <=: BNS = ten * 0.1 + (I-10) * 0.075elif < I <=: BNS = ten * 0.1 + ten * 0.075 + (I-20) * 0.05elif < I <=: BNS = 10 * 0.1+ * 0.075 + * 0.05 + (I-40) * 0.03elif < I <=: BNS = 10 * 0.1+ 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + ( I-60) * 0.015elif I >: BNS = ten * 0.1 + ten * 0.075 + * 0.05 + * 0.03 + + * 0.015 + (I-100) * 0.015print ( BNS, ' million Yuan ')
But later, check the other people's practice, sure enough fresh and many AH:
I = Float (Enter the profit for the month, in Million: ")) BNS = 0 #初始化奖金总数cat = [+, 0], #金额分6档pct = [0.01, 0.015, 0.03, 0 .0.075, 0.1] #每档的提成比例for i in range (6): If i > cat[i]: i = i-cat[i] #注意: After planing out the amount of the gear, the value should be re-assigned to I, each stall is calculated separately BNS = bns + I * Pct[i] #每一档位的奖金累计相加print (' Total bonus for the month is%s million '% BNS)
It is not difficult to understand: from high to low check whether the profit is more than the set of gear, super-first calculate this part of the bonus, the remaining to continue to step-by-step check stalls, the cumulative calculation of the bonus can be.
Alas, I'm really bad at maths ...
PS: Why the last line in the Code box always shows an exception ... It turned out to be a template problem. Change a template, the code box appears normal ~ ~
Python exercises 002: Bonus calculation