Int不可迭代的错误

时间:2016-11-13 09:06:41

标签: python

我正在学习python在线并且需要提交一个解决方案,我已经在本地和repl.it上尝试了我的代码并且代码运行但是在线分级器的unittest引发了错误:

"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)

我很高兴知道我的代码是否有任何问题。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)