Summarize several ways of writing if in the python learning process

高洛峰
Release: 2017-03-10 17:01:55
Original
2716 people have browsed it

This article summarizes several ways of writing if in the python learning process

Small problems encountered in python example learning, I changed the topic to display the results of each file, you can use lists and if To complete the statement, the article ends with the original question and answer

1. If method

A. Program

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

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]

if arr[idx] == 0:
print('<100000','file',(i - arr[idx]) * rat[idx])
else:
print(arr[idx],'file',(i - arr[idx]) * rat[idx])
i = arr[idx]

print('Total bonus:',r)


D:\PythonScript\python\python.exe D:/PythonScript/456
Net profit :1100000
1000000 file 1000.0
600000 file 6000.0
400000 file 6000.0
200000 file 10000.0
100000 file 7500.0
<100000 file 10000.0
Total bonus: 40500.0

Process finished with exit code 0

B. Program

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

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]

if arr[idx] == 0:
print('<100000','file',(i - arr[idx]) * rat[idx])
                                                                                                                                                                                                                                 . Total bonus: ',r)


B. Result

D:\PythonScript\python\python.exe D:/PythonScript/456

Net profit: 1100000

1100000 file 1000.0

1000000 file 6000.0

600000 file 6000.0

400000 file 10000.0

200000 file 7500.0

<100000 level 10000.0

Total bonus: 40500.0

Process finished with exit code 0

## C. Program


# !/usr/bin/python

# -*- coding: UTF-8 -*-

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(arr[idx] if arr[idx ]>0 else '<100000','file',(i - arr[idx]) * rat[idx])
i = arr[idx]

print('Total bonus: ',r)





C. Result


##D:\PythonScript\python\python.exe D:/ PythonScript/456

Net profit: 1100000

1000000 file 1000.0

600000 file 6000.0

400000 file 6000.0

200000 file 10000.0

100000 Level 7500.0

<100000 Level 10000.0

Total bonus: 40500.0

Process finished with exit code 0

2. List method

Program

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


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]
fff = ['Sixth gear','Fifth gear ','Fourth gear','Third gear','Second gear','First gear']
r = 0
for idx in range(0,6):
if i> ;arr[idx]:
r+=(i-arr[idx])*rat[idx]
print (fff[idx],(i-arr[idx])*rat[idx])
       i=arr[idx]
print ('Total bonus:',r)

Result

D:\PythonScript\python\python.exe D:/PythonScript /123.py

Net profit: 1100000

The sixth gear 1000.0

The fifth gear 6000.0

The fourth gear 6000.0

Third level 10000.0

Second level 7500.0

First level 10000.0

Total bonus: 40500.0

Process finished with exit code 0


2. Original question and answer

Question: The bonuses issued by the company are based on profits. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is greater than 100,000 yuan and less than 200,000 yuan, the portion less than 100,000 yuan is subject to a 10% commission, and if the profit is greater than 100,000 yuan, the bonus is 10%. For the part between 200,000 and 400,000, the commission is 5% for the part above 200,000 yuan; for the part above 400,000 yuan between 400,000 and 600,000, the commission is 3% ; When it is between 600,000 and 1 million, the portion above 600,000 yuan can be commissioned at 1.5%. When it is above 1 million yuan, the portion over 1 million yuan is 1% commission. Enter the profit I of the current month from the keyboard. Please respond. Total number of bonuses distributed?

Program analysis: Please use the number axis to divide and position. Note that the bonus needs to be defined as an integer when defining.

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 r

The output result of the above example is:

Net profit: 120000

1500.0

10000.0

11500.0


The above is the detailed content of Summarize several ways of writing if in the python learning process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!