2

## python入門題

54萬元以下免稅，大於54萬至121萬稅率12%,大於121萬至242萬稅率20%,

ccutmis iT邦高手 4 級 ‧ 2020-03-30 08:39:36 檢舉
#提供您學習參考
salary=input("請輸入薪資")
if salary>1031萬:
tax=0.45
elif salary<=1031萬 and salary>453萬:
tax=0.4
elif salary<=453萬 and salary>242萬:
tax=0.3
…中間省略…
elif salary<=121萬 and salary>54萬:
tax=0.12
else:
tax=0 #這是註解,小於等於54萬免稅
print(tax)

1.了解自己的需求
2.查資料
3.思考並嘗試
4.求助於他人

### 2 個回答

2
japhenchen
iT邦超人 1 級 ‧ 2020-03-30 07:58:24
``````tax = 0
if salary >= 540000 :
tax = 0.12
if salary >= 1210000 :
tax = 0.2
if salary >= 2420000:
tax = 0.3
if salary >= 4530000:
tax = 0.4
if salary >=10310000:
tax = 0.45
else :  #這個ELSE有點脫褲子放屁了
tax = 0
``````

c用 { } 來指定流程區塊
psacal 用 begin end
python 用冒號加縮進(我慣用TAB)

``````tax = 0 if salary < 540000 else 0.12 if salary < 1210000 else 0.2 if salary < 2420000 else 0.3 if salary < 4530000 else 0.4 if salary < 10310000 else 0.45
``````
echochio iT邦高手 1 級 ‧ 2020-03-30 11:55:08 檢舉

0

iT邦新手 5 級 ‧ 2021-05-24 16:19:22

#### Python

``````def tax(mon):
if mon > 10310000:
return 0.45
elif mon <= 10310000 and mon > 4530000:
return 0.4
elif mon <= 4530000 and mon > 2420000:
return 0.3
elif mon <= 2420000 and mon > 1210000:
return 0.2
elif mon <= 1210000 and mon > 540000:
return 0.12
elif mon <= 540000 and mon >= 0:
return 0
elif mon < 0:
return "收入不得為負！"
else:
return "計算錯誤！"
def int_to_zh(mon):
if str(mon)[-2:] == ".0":
mon = str(mon)[:-2]
if type(mon) != "float":
mon = str(mon)
mon = mon[:-16]+mon[-16:].replace("0000000000000000", "京")
mon = mon[:-12]+mon[-12:].replace("000000000000", "兆")
mon = mon[:-8]+mon[-8:].replace("00000000", "億")
mon = mon[:-4]+mon[-4:].replace("0000", "萬")
return mon[:-3]+mon[-3:].replace("000", "千")
else:
return mon
mon = str(input("請輸入您的收入："))
try:
mon = int(mon.replace("元", "").replace("百", "00").replace("千", "000").replace("萬", "0000").replace("十萬", "00000").replace("百萬", "000000").replace("千萬", "0000000").replace("億", "00000000").replace("十億", "000000000").replace("百億", "0000000000").replace("千億", "00000000000").replace("兆", "000000000000").replace("十兆", "0000000000000").replace("百兆", "00000000000000").replace("千兆", "000000000000000").replace("京", "0000000000000000"))
# print(mon)
except ValueError:
print("收入須為整數！")
exit()
try:
print("您的收入為"+int_to_zh(mon)+"元", "稅率為"+str(tax(mon)*100)[:-2]+"%", "稅額為"+int_to_zh(mon*tax(mon))+"元", "剩餘金額為"+int_to_zh(mon-mon*tax(mon))+"元", sep="｜")
except:
print(tax(mon))
``````

#### Run

``````請輸入您的收入：453萬

``````

code有點亂，對初學者來說功能有到即可？