调试 Python 计算税码
Debugging Python Calculate Tax Code
下面的代码应该计算不同人从字典中读取的所得税并将他们的税返回到字典中,这是我在回答某个测验时编写的;
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}
}
谁能帮我看看我的代码有什么问题?
以下是我收到的错误信息。
您的代码中有一个 ERROR/BUG
结果:
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
您的代码中有一个非 ascii 字符。看着它我会说这是你使用的引号。将它们替换为常规引号:
注意:如果您想在字符串或其他内容中使用非 ascii 字符,您需要在顶部包含文件编码,但只有在需要时才这样做
# -*- coding: utf-8 -*-
在代码的这一行:
#income = {‘Alex’: 500, ‘James’: 20500,‘Kinuthia’: 70000}
不要使用反引号,使用普通引号并记住这是 Python,而不是 MySQL 数据库.
下面的代码应该计算不同人从字典中读取的所得税并将他们的税返回到字典中,这是我在回答某个测验时编写的; 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}
}
谁能帮我看看我的代码有什么问题?
以下是我收到的错误信息。 您的代码中有一个 ERROR/BUG 结果:
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
您的代码中有一个非 ascii 字符。看着它我会说这是你使用的引号。将它们替换为常规引号:
注意:如果您想在字符串或其他内容中使用非 ascii 字符,您需要在顶部包含文件编码,但只有在需要时才这样做
# -*- coding: utf-8 -*-
在代码的这一行:
#income = {‘Alex’: 500, ‘James’: 20500,‘Kinuthia’: 70000}
不要使用反引号,使用普通引号并记住这是 Python,而不是 MySQL 数据库.