Python exercise 002: bonus calculation, python002

Source: Internet
Author: User

Python exercise 002: bonus calculation, python002

[Python exercise 002]The enterprise's bonus is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%; if the profit is higher than 0.1 million yuan, if the profit is lower than 0.2 million yuan, the portion lower than 0.1 million yuan will be charged by 10%, and the portion higher than 0.1 million yuan, 7.5% for Coco; 0.2 million for a part higher than 0.4 million yuan between 0.2 million and 5%; 0.4 million for a part higher than 0.6 million yuan between 0.4 million and 3%, or for a part higher than yuan; between 0.6 million and 1 million, if the price is higher than 0.6 million yuan, the price can be increased to 1.5%. If the price is higher than 1 million yuan, the price of more than 1 million yuan is increased by 1%, and the current month's profit is input from the keyboard, how many bonuses should be paid?

Well, what I can think of is to write the loop function of if... elif... honestly. I feel tired when I look at it myself ......

I = float (input ("Please input the profit for the current month, in the unit of 10 million:") if I <= 10: bns = 10*0.1 elif 10 <I <= 20: bns = 10*0.1 + (I-10) * 0.075 elif 20 <I <= 40: bns = 10*0.1 + 10*0.075 + (I-20) * 0.05 elif 40 <I <= 60: bns = 10*0.1 + 10*0.075 + 20*0.05 + (I-40) * 0.03 elif 60 <I <= 100: bns = 10*0.1 + 10*0.075 + 20*0.05 + 20*0.03 + (I-60) * 0.015 elif I> 100: bns = 10*0.1 + 10*0.075 + 20*0.05 + 20*0.03 + 40*0.015 + (I-100) * 0.015 print (bns, '000000 ')

 

However, I checked other people's practices, which is actually a lot fresh:

I = float (input ("Please input the profit for the current month, in the unit of 10 million yuan:") bns = 0 # total initial bonuses cat = [100, 60, 40, 20, 10, 0] # amount score 6 pct = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1] # Percentage of Commission for I in range (6 ): if I> cat [I]: I = I-cat [I] # Note: after the amount of the gear is cleared, the value should be assigned to I again, calculate bns = bns + I * pct [I] # sum of the bns = bns + I * pct [I] # print ('the total number of bonuses payable in the current month is % s '% bns)

  

It is not hard to understand: Check whether the profit from high to low has exceeded the configured position. If it exceeds the configured position, calculate the bonus, and check the position level by level, you can accumulate the computing bonus.

Alas, I am really bad at mathematics ......

PS: Why is an exception always displayed in the last line in the code box ...... It turned out to be a template issue. After a template is changed, the code box is displayed as normal ~~

 

++ ++

Source: getting started with programming languages: 100 typical examples [Python]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.