2016-11-13 102 views
0

我在网上学习Python和需要提交一个解决方案,我已经尝试了本地我的代码和repl.it和代码运行,但在单元测试,然后在线平地机抛出一个错误:诠释不迭代错误

"THERE IS AN ERROR/BUG IN YOUR CODE 
Results: 
Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, TypeError("'int' object is not iterable",),), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable" 

我的代码如下:

def calculate_tax(income2Tax): 
    zeroPercentRate = 0.0 
    tenPercentRate = 0.1 
    fifteenPercentRate = 0.15 
    twentyPercentRate = 0.20 
    twenty5PercentRate = 0.25 
    thirtyPercentRate = 0.30 

    ten = 9000 
    fifteen = 10200 
    twenty = 10550 
    twenty5 = 19250 

    taxedIncome = {} 
    rate = 0 
    for name in income2Tax: 
     if (income2Tax[name] <= 1000): 
      rate = 0 
      taxedIncome[name] = rate 

     elif (income2Tax[name] >= 1001) and (income2Tax[name] <= 10000): 
      rate = ((income2Tax[name] - 1000) * tenPercentRate) 
      taxedIncome[name] = rate 

     elif (income2Tax[name] >= 10001) and (income2Tax[name] <= 20200): 
      rate = (ten * tenPercentRate) + (income2Tax[name] - 10000) * fifteenPercentRate 
      taxedIncome[name] = rate 

     elif (income2Tax[name] >= 20201) and (income2Tax[name] <= 30750): 
      rate = (ten * tenPercentRate) + (fifteen * fifteenPercentRate) + (income2Tax[name] - 20200) * twentyPercentRate 
      taxedIncome[name] = rate 

     elif (income2Tax[name] >= 30751) and (income2Tax[name] <= 50000): 
      rate = (ten * tenPercentRate) + (fifteen * fifteenPercentRate) + (twenty * twentyPercentRate) + (income2Tax[name] -30750) * twenty5PercentRate 
      taxedIncome[name] = rate 

     elif (income2Tax[name] > 50000): 
      rate = (ten * tenPercentRate) + (fifteen * fifteenPercentRate) + (twenty * twentyPercentRate) + (twenty5 * twenty5PercentRate) + (income2Tax[name] - 50000) * thirtyPercentRate 
      taxedIncome[name] = rate 

     return taxedIncome 

sampleData = {'bob': 500, 'bill': 20500, 'bale': 70000, 'bub': 8000, 'bit':19000} 
print calculate_tax(sampleData) 

我将不胜感激知道,如果有什么错我的代码。

+0

你怎么不知道,如果你的代码的工作?您是否尝试过在本地进行测试?你是怎么写的? – jonrsharpe

+0

代码在本地运行(ubuntu 16.10)并在repl.it编译器上运行,但在课程站点上运行时生成错误。 – driftavalii

回答

1

这一个返回一个列表:

ten = 9000 
fifteen = 10200 
twenty = 10550 
twenty5 = 19250 

def calculate_tax(data): 
    taxData = {} 
    for name, amount in data.iteritems(): 


     if amount <= 1000: 
      taxedIncome = 0 

     elif amount >= 1001 and amount <= 10000: 
      taxedIncome = ((amount - 1000) * 0.1) 

     elif amount >= 10001 and amount <= 20200: 
      taxedIncome = (ten * 0.1) + (amount - 10000) * 0.15 

     elif amount >= 20201 and amount <= 30750: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (amount - 20200) * 0.2 

     elif amount >= 30751 and amount <= 50000: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (twenty * 0.2) + (amount -30750) * 0.25 

     elif amount > 50000: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (twenty * 0.2) + (twenty5 * 0.25) + (amount - 50000) * 0.3 

     taxData[name] = taxedIncome 

    return taxData 


sampleData = {'bob': 500, 'bill': 20500, 'bale': 70000, 'bub': 8000, 'bit':19000} 
print calculate_tax(sampleData) 
+0

感谢您的代码,它运行并返回结果,但仍未被接受。 – driftavalii

+0

为什么代码不被接受? – Ukimiku

+0

这是一个自动评估程序,并生成以下错误:'您的代码中存在错误/错误 结果: 内部错误:runTests中止:TestOutcomeEvent(句柄= False,test =,result =,outcome ='错误',exc_info =(,AttributeError(“'int'object has no attribute'iteritems'”,),),reason = None,expected = False,shortLabel = None,longLabel = None)不是JSON序列化 {'bub' :700.0,'bale':15352.5,'bob':0,'bill':2490.0,'bit':2250.0}' – driftavalii

0

试试这个:

ten = 9000 
fifteen = 10200 
twenty = 10550 
twenty5 = 19250 

def calculate_tax(data): 
    taxData = [] 
    for name, amount in data.iteritems(): 


     if amount <= 1000: 
      taxedIncome = 0 

     elif amount >= 1001 and amount <= 10000: 
      taxedIncome = ((amount - 1000) * 0.1) 

     elif amount >= 10001 and amount <= 20200: 
      taxedIncome = (ten * 0.1) + (amount - 10000) * 0.15 

     elif amount >= 20201 and amount <= 30750: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (amount - 20200) * 0.2 

     elif amount >= 30751 and amount <= 50000: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (twenty * 0.2) + (amount -30750) * 0.25 

     elif amount > 50000: 
      taxedIncome = (ten * 0.1) + (fifteen * 0.15) + (twenty * 0.2) + (twenty5 * 0.25) + (amount - 50000) * 0.3 

     t = (name, taxedIncome) 
     taxData.append(t) 

    return taxData 


sampleData = {'bob': 500, 'bill': 20500, 'bale': 70000, 'bub': 8000, 'bit':19000} 
print calculate_tax(sampleData) 
+0

这会返回一个列表吗?我想返回一个字典,它可以在字典上工作吗? – driftavalii

+0

Iteritems()在列表上工作。在你的代码中,sampleData是一个列表。 iteritems()遍历该列表。我选择返回一个列表,对。但是你可以轻松地返回一个字典。但是,如果你有几个“鲍勃”或“比尔”,字典方法将不再工作,因为字典要求键(在你的案例中的名称)是唯一的。 – Ukimiku

相关问题