python实例: 税率计算
2017-05-27 本文已影响115人
随风化作雨
直接上源码:
#!/usr/bin/env python
# -*- coding: utf-8 -*
class tax(object):
'''
Get the tax you want to know!
'''
def init_input(self):
while True:
try:
print '*'*60
input1 = raw_input("Your pretax salary (month):\n")
if float(input1)>0:
self.pretax = float(input1)
#print "*"*40
break
else:
print "The pretax salary must large than zero"
continue
except (ValueError,TypeError):
print "Your input is not right!"
while True:
try:
input2 = raw_input("The base salary for insurance:")
if input2=='':
self.ins_base = self.pretax
print self.ins_base
break
else:
if float(input2)>0:
self.ins_base = float(input2)
#print "*"*40
break
else:
print "The base salary for insurance must large than zero"
continue
except (ValueError,TypeError):
print "Your input is not right!"
while True:
try:
input3 = raw_input("The base salary for housing found:")
if input3=='':
self.hf_base = self.pretax
print self.hf_base
print "*"*40
break
elif input1 == 'exit':
break
else:
if float(input3)>0:
self.hf_base = float(input3)
print "*"*40
break
else:
print "The base salary for housing found must large than zero"
continue
except (ValueError,TypeError):
print "Your input is not right!"
def insurance(self):
self.ins_UP = 15669 #五险最大基数
self.endowment_ins = max([min([self.ins_base,self.ins_UP]),2089])*0.08 #养老保险
self.medical_ins = max([min([self.ins_base,self.ins_UP]),3134])*0.02+3 #医疗保险
self. unemployment_ins = max([min([self.ins_base,self.ins_UP]),2089])*0.005 #失业保险
self.injury_ins = 0 #工伤保险
self.maternity_ins = 0 #生育保险
self.sum_ins = self.endowment_ins + self.medical_ins + self.unemployment_ins + self.injury_ins + self.maternity_ins
return self.sum_ins
def housingFound(self):
self.hf_UP = 21258
self.sum_hf = min([self.hf_base,self.hf_UP])*0.12
return self.sum_hf
def tax(self):
self.threshold = 3500
#self.threshold = 4800
self.tax_base = self.pretax-self.sum_ins-self.sum_hf-self.threshold
if self.tax_base<=0:
self.tax = 0
elif self.tax_base>0 and self.tax_base<=1500:
self.tax = self.tax_base*0.03
elif self.tax_base>1500 and self.tax_base<=4500:
self.tax = self.tax_base*0.1-105
elif self.tax_base>4500 and self.tax_base<=9000:
self.tax = self.tax_base*0.2-505
elif self.tax_base>9000 and self.tax_base<=35000:
self.tax = self.tax_base*0.25-1005
elif self.tax_base>35000 and self.tax_base<=55000:
self.tax = self.tax_base*0.3-2755
elif self.tax_base>55000 and self.tax_base<=80000:
self.tax = self.tax_base*0.35-5505
elif self.tax_base>80000 :
self.tax = self.tax_base*0.45-13505
return self.tax
def afterTax(self):
self.after_tax = self.pretax-self.sum_ins-self.sum_hf-self.tax
return self.after_tax
def companyPay(self):
self.comp_UP = 15669 #五险最大基数
self.endowment_comp = max([min([self.ins_base,self.comp_UP]),2089])*0.2 #养老保险
self.medical_comp = max([min([self.ins_base,self.comp_UP]),3134])*0.1 #医疗保险
self. unemployment_comp = max([min([self.ins_base,self.comp_UP]),2089])*0.01 #失业保险
self.injury_comp = max([min([self.ins_base,self.comp_UP]),3134])*0.005 #工伤保险
self.maternity_comp = max([min([self.ins_base,self.comp_UP]),3134])*0.008 #生育保险
self.sum_comp = self.endowment_comp + self.medical_comp + self.unemployment_comp + self.injury_comp + self.maternity_comp
self.companyPay = self.sum_hf+self.sum_comp+self.pretax
return self.companyPay
def main():
while True:
mytax = tax()
mytax.init_input()
insuranceU = mytax.insurance()
hfU = mytax.housingFound()
taxU = mytax.tax()
after_taxU = mytax.afterTax()
companyPayU = mytax.companyPay()
print "-"*8
print "person:"
print "-"*8
print "%25s: %-10.2f" %('salary',mytax.pretax)
#print "%15s: %-10.2f" %('insurance',insuranceU)
print "%25s: %-10.2f" %('endowment insurance',mytax.endowment_ins)
print "%25s: %-10.2f" %('medical insurance',mytax.medical_ins)
print "%25s: %-10.2f" %('unemployment insurance',mytax.unemployment_ins)
print "%25s: %-10.2f" %('injury insurance',mytax.injury_ins)
print "%25s: %-10.2f" %('maternity insurance',mytax.maternity_ins)
print "%25s: %-10.2f" %('housing found',hfU)
print "%25s: %-10.2f" %('tax',taxU)
print "%25s: %-10.2f" %('after tax',after_taxU)
print "-"*8
print "company:"
print "-"*8
print "%25s: %-10.2f" %('companyPay',companyPayU)
print "%25s: %-10.2f" %('salary',mytax.pretax)
#print "%25s: %-10.2f" %('insurance',mytax.sum_comp)
print "%25s: %-10.2f" %('endowment insurance',mytax.endowment_comp)
print "%25s: %-10.2f" %('medical insurance',mytax.medical_comp)
print "%25s: %-10.2f" %('unemployment insurance',mytax.unemployment_comp)
print "%25s: %-10.2f" %('injury insurance',mytax.injury_comp)
print "%25s: %-10.2f" %('maternity insurance',mytax.maternity_comp)
print "%25s: %-10.2f" %('housing found',hfU)
if __name__ == '__main__':
main()
运行结果 :