最近在学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)))