Python Exercise Example 2
**Question:** An enterprise distributes bonuses based on profit commissions. When profit (I) is less than or equal to 100000, the bonus can be 10%. When profit is above 100000 but below 200000, the portion below 100000 gets 10% and the portion above 100000 gets 7.5%. When between 200000 and 400000, the portion above 200000 gets 5%. When between 400000 and 600000, the portion above 400000 gets 3%. When between 600000 and 1000000, the portion above 600000 gets 1.5%. When above 1000000, the portion above 1000000 gets 1%. Input the monthly profit I from the keyboard and calculate the total bonus to be paid.
**Program analysis:** Use a number line to divide and locate intervals.
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
]
(
i-arr
[
idx
]
)
*rat
[
idx
]
i
=
arr
[
idx
]
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
]
(
(
i-arr
[
idx
]
)
*rat
[
idx
]
)
i
=
arr
[
idx
]
(
r
)
The output of the above example is:
净利润:120000
1500.0
10000.0
11500.0
Python 100 Examples