Python Practice Example 2

Python 100 Examples

Question:The bonus issued by an enterprise is based on the profit commission rate. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be 10% of the profit; when the profit is higher than 100,000 yuan but lower than 200,000 yuan, the part below 100,000 yuan is commissioned at 10%, and the part above 100,000 yuan can be commissioned at 7.5%; when it is between 200,000 and 400,000 yuan, the part above 200,000 yuan can be commissioned at 5%; when it is between 400,000 and 600,000 yuan, the part above 400,000 yuan can be commissioned at 3%; when it is between 600,000 and 1,000,000 yuan, the part above 600,000 yuan can be commissioned at 1.5%; when it is higher than 1,000,000 yuan, the part exceeding 1,000,000 yuan is commissioned at 1%. Enter the monthly profit I from the keyboard and calculate the total bonus to be paid.

Program analysis:Please use the number line to demarcate boundaries and locate.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

i = int(raw_input('Net profit:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
    if i>arr[idx]:
        r+=(i-arr[idx])*rat[idx]
        print (i-arr[idx])*rat[idx]
        i=arr[idx]
print r

Example (Python 3.0+)

#!/usr/bin/python3
 
i = int(input('Net profit:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
    if i>arr[idx]:
        r+=(i-arr[idx])*rat[idx]
        print ((i-arr[idx])*rat[idx])
        i=arr[idx]
print (r)

The output of the above example is:

净利润:120000
1500.0
10000.0
11500.0

Python 100 Examples

Other Extensions