1. 程式人生 > >python-計算個人所得稅

python-計算個人所得稅

except eas %d sel jin return lease error python2

最近在學python,寫了個計算個人所得稅計算的腳本,分享。

以下為python3適用版本

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 該python腳本用於計算稅後工資

# 提示用戶輸入工資
sal = input("Please input your salary: \n") 

# 自定義一個異常
class MyException(Exception): 
    def __init__(self, *args):
        self.args = args
 
try:
	sal = int(sal)
	if sal < 0:
		raise MyException			
except ValueError:
	print("給勞資輸一個整數,別帶字母和小數!\n")
except MyException:
	print("你大爺的,工資是負數?\n")		
else:
	# 計算社保和公積金以及扣除該部分之後的工資
	shebao = round(sal * 0.105, 2)
	gongjijin = round(sal * 0.07, 2)
	aft_sal = sal - shebao - gongjijin
	
	# 稅率(這個沒變)
	sui_lv = [0.45, 0.35, 0.3, 0.25, 0.2, 0.1, 0.03]
	
	# 稅改前超出起征點各階段工資
	jin_e1 = [80000, 55000, 35000, 9000, 4500, 1500, 0]
	
	# 稅改後超出起征點各階段工資
	jin_e2 = [80000, 55000, 35000, 25000, 12000, 3000, 0]
	
	# 起征點(調整前3500,調整後5000)
	qizheng1 = 3500
	qizheng2 = 5000
	
	# 應繳稅工資:稅前工資-社保-公積金-起征點
	yinjiao1 = aft_sal - qizheng1
	yinjiao2 = aft_sal - qizheng2
	
	# 定義計算所得稅函數
	# a應繳稅工資,b超出起征點各階段工資,c稅率
	def jisuan(a, b, c):
		sum = 0
		for i in range(0, len(b)):
			if a > b[i]:
				sum += (a - b[i]) * c[i]
				a = b[i]
		return round(sum, 2)
		
	# 計算所得稅	
	suodesui1 = jisuan(yinjiao1, jin_e1, sui_lv)
	suodesui2 = jisuan(yinjiao2, jin_e2, sui_lv)
	
	print("\n稅前月收入: {}".format(sal))
	print("五險一金: {}".format(shebao + gongjijin))
	print("舊稅率應納稅: {} 元,稅後收入: {} 元".format(suodesui1, round(aft_sal-suodesui1, 2)))
	print("新稅率應納稅: {} 元,稅後收入: {} 元\n".format(suodesui2, round(aft_sal-suodesui2, 2)))

--------------------------------------------------------分割線------------------------------------------------------------------------------------

以下為python2適用的版本

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 該python腳本用於計算稅後工資

# 提示用戶輸入工資
sal = raw_input("Please input your salary: \n")

# 自定義一個異常
class MyException(Exception): 
    def __init__(self, *args):
        self.args = args
 
try:
	sal = int(sal)
	if sal < 0:
		raise MyException			
except ValueError:
	print("給勞資輸一個整數,別帶字母和小數!\n")
except MyException:
	print("你大爺的,工資是負數?\n")		
else:
	# 計算社保和公積金以及扣除該部分之後的工資
	shebao = round(sal * 0.105, 2)
	gongjijin = round(sal * 0.07, 2)
	aft_sal = sal - shebao - gongjijin
	
	# 稅率(這個沒變)
	sui_lv = [0.45, 0.35, 0.3, 0.25, 0.2, 0.1, 0.03]
	
	# 稅改前超出起征點各階段工資
	jin_e1 = [80000, 55000, 35000, 9000, 4500, 1500, 0]
	
	# 稅改後超出起征點各階段工資
	jin_e2 = [80000, 55000, 35000, 25000, 12000, 3000, 0]
	
	# 起征點(調整前3500,調整後5000)
	qizheng1 = 3500
	qizheng2 = 5000
	
	# 應繳稅工資:稅前工資-社保-公積金-起征點
	yinjiao1 = aft_sal - qizheng1
	yinjiao2 = aft_sal - qizheng2
	
	# 定義計算所得稅函數
	# a應繳稅工資,b超出起征點各階段工資,c稅率
	def jisuan(a, b, c):
		sum = 0
		for i in range(0, len(b)):
			if a > b[i]:
				sum += (a - b[i]) * c[i]
				a = b[i]
		return round(sum, 2)
	
	# 計算所得稅
	suodesui1 = jisuan(yinjiao1, jin_e1, sui_lv)
	suodesui2 = jisuan(yinjiao2, jin_e2, sui_lv)
	
	print("\n稅前月收入: %d" % sal)
	print("五險一金: %.2f" % (shebao + gongjijin))
	print("舊稅率應納稅: %.2f 元,稅後收入: %.2f 元" % (suodesui1, (aft_sal-suodesui1)))
	print("新稅率應納稅: %.2f 元,稅後收入: %.2f 元\n" % (suodesui2, (aft_sal-suodesui2)))
	

  

  

python-計算個人所得稅