這篇文章總結python學習過程中if的幾種寫法
python實例學習中遇到的小問題,我對題目改動一下需要顯示每一步的結果,可以用列表和if語句來完成,文章最後是原題目和答案
一、if的方式
#A、程式
# !/usr/bin/python
# -*- coding: UTF-8 -*-
i = int(input('淨利:'))
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:
if - arr[idx]) * rat[idx])
else:
print(arr[idx],'檔',(i - arr[ arr[idx]
print('獎金總計:',r)
D:\PythonScript\python\python.exe D:/PythonScript/456
淨利潤:1100000
1000000 檔 1000.0
600000 檔 6000.0
400000 0 檔 10000.0
獎金總計: 40500.0
Process finished with exit code 0
B、程式
# !/usr/bin/python
arr = [1000000, 600000, 400000, 200000, 100000,000]
101 , 0.075, 0.1]
r = 0
for idx in range(0, 6):
if i > * rat[idx]
if arr[idx] == 0:
print('<1000000877719707070707070(L07070070707007000700000085((x),00000829797970000(x)')(70005()'2005()'21007000070700005(F). ## else:
print(arr[idx] ,'檔',(i - arr[ #print('獎金總計:',r)
B、結果
D:\PythonScript\python\python.exe D:/PythonScript/456
淨利: 1100000
1100000 檔1000.0
C、程式
# !/usr/bin/python
# -*- coding: UTF-8 -*-i = int(input('淨利:') )arr = [1000000, 600000, 400000, 200000, 100000, 0]
rat = [0.01, 0.015, 0]rat = [0.01, 0.015, 0]
rat = [0.01, 0.015, 0]
if i > arr[idx]:
r += (i - arr[idx ]>0 else '<100000','檔',(i - arr[idx]) * rat[idx])i = arr[idx]
##1( ',r)
C、結果
D:\PythonScript\python\python.exe D:/ PythonScript/456
淨利:1100000
1000000 檔1000.0
400000 檔6000.0
#20000.
##100000 檔7500.0<100000 檔 10000.0<100000 檔10000.0
<100000 檔10000.0#1二、列表的方式程式#!/usr/bin/python
# -*- coding: UTF-8 -*-
i = int(input('淨利潤:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]##fff ','第四檔','第三檔','第二檔','第一檔']
r = 0
for idx in range(0,6):
if i> ;arr[idx]:
r+=(i-arr[idx])*rat[idx]
print (fff[idx],(i-arr[idx])*#[idx])*#] # i=arr[idx]
print ('獎金總數:',r)
結果
#D:\PythonScript\python\python.exe D:PythonScript
#D:\PythonScript\python\python.exe D:PythonScript? /123.py淨利潤:1100000第六檔1000.0第五檔6000.0第四檔6000.0第三檔10000.0第二檔7500.0第一檔10000.0總獎金:40500.0Process finished with exit code 0二、原題目與解答
## rat = [0.01,0.015,0.03,0.05,0.075,0.1]r = 0
for idx in range(0,6):
##if i>:#if#r+=(i-arr[idx])*rat[idx]
print (i-arr[idx])*rat[idx]
i=arr[idx]
i=arr[idx ]print r
以上實例輸出結果為:
淨利:120000
1500.010000.0
11500.0
#
以上是總結python學習過程中if的幾種寫法的詳細內容。更多資訊請關注PHP中文網其他相關文章!