调试Python计算税码

时间:2016-09-06 09:15:02

标签: python-3.x tax

以下代码应该为不同的人从字典中读取并将税收返还给字典来计算所得税,这是我在回答某个测验时写的; 国家X使用分级比率计算其公民的税,如下所示:

年收入:0 - 1000

税率:0%

年收入:1,001 - 10,000

税率:10%

年收入:10,001 - 20,200

税率:15%

年收入:20,201 - 30,750

税率:20%

年收入:30,751 - 50,000

税率:25%

年收入:超过50,000

税率:30%

{

    tax = {}
    def calculate_tax(income):
      for key, value in income.items():
        if value <= 1000:
          income_tax = 0
          tax = {key : income_tax}
        elif value in range(1001,10000):
          tax1 = 0 
          tax2 = 0.1 * (value - 1000)
          income_tax = tax1 + tax2
          tax = {key : income_tax}
        elif value in range(10001,20200):
          tax1 = 0
          tax2 = 0.1 *9000
          tax3 = 0.15 * (value - 10000)
          income_tax = tax1+tax2+tax3
          tax = {key : income_tax}
        elif value in range(20201,30750):
          tax1 = 0
          tax2 = 0.1 * 9000
          tax3 = 0.15 * 10200
          tax4 = 0.20 * (value - 20200)
          income_tax = tax1+tax2+tax3+tax4
          tax = {key : income_tax}
        elif value in range(30751,50000):
          tax1 = 0
          tax2 = 0.1 * 9000
          tax3 = 0.15 * 10200
          tax4 = 0.20 * 10550
          tax5 = 0.25 * (value - 30750)
          income_tax = tax1+tax2+tax3+tax4+tax5
          tax = {key : income_tax}
        elif value > 50000:
          tax1 = 0
          tax2 = 0.1 * 9000
          tax3 = 0.15 * 10200
          tax4 = 0.20 * 10550
          tax5 = 0.25 * 19250
          tax6 = 0.3 * (value - 50000)
          income_tax = tax1+tax2+tax3+tax4+tax5+tax6
          tax = {key : income_tax}
      return tax
    income = {‘Alex’: 500, ‘James’: 20500,‘Kinuthia’: 70000}

}

有人可以帮我告诉我代码有什么问题吗?

以下是我收到的错误消息。 您的代码中存在错误/错误 结果:

Traceback (most recent call last):
  File "python/nose2/bin/nose2", line 8, in 
    discover()
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 300, in discover
    return main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 100, in __init__
    super(PluggableTestProgram, self).__init__(**kw)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 133, in parseArgs
    self.createTests()
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 258, in createTests
    self.testNames, self.module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/loader.py", line 67, in loadTestsFromNames
    for name in event.names]
  File "/usr/local/lib/python2.7/dist-packages/nose2/loader.py", line 82, in loadTestsFromName
    result = self.session.hooks.loadTestsFromName(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2/events.py", line 224, in __call__
    result = getattr(plugin, self.method)(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2/plugins/loader/testclasses.py", line 119, in loadTestsFromName
    result = util.test_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/util.py", line 106, in test_from_name
    parent, obj = object_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/util.py", line 117, in object_from_name
    module = __import__('.'.join(parts_copy))
  File "/home/ubuntu/Applications/andelabs-server/tmp/56cefbb14fa1511500d5b3b6-57bc2762e713b01900df735c-test.py", line 4, in 
    from tmp.andelabs_56cefbb14fa1511500d5b3b6_57bc2762e713b01900df735c import *
  File "/home/ubuntu/Applications/andelabs-server/tmp/andelabs_56cefbb14fa1511500d5b3b6_57bc2762e713b01900df735c.py", line 43
SyntaxError: Non-ASCII character '\xe2' in file /home/ubuntu/Applications/andelabs-server/tmp/andelabs_56cefbb14fa1511500d5b3b6_57bc2762e713b01900df735c.py on line 43, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

2 个答案:

答案 0 :(得分:0)

代码中有非ascii字符。看着它,我说它是你正在使用的引号。用常规引号替换它们:

注意:如果您想在字符串中使用非ascii字符,或者您需要在顶部包含文件编码,但只有在需要时才执行此操作

my_downcase.call(string,range_from,range_to)

答案 1 :(得分:0)

在您的代码中的这一行:

#income = {‘Alex’: 500, ‘James’: 20500,‘Kinuthia’: 70000}

不要使用后退标记,使用正常的引号并记住这是 Python ,而不是 MySQL 数据库。