driftavalii
driftavalii

Reputation: 1339

Int not iterable error

I am learning python online and need to submit a solution, I have tried my code locally and on repl.it and the code runs but the unittest of then online grader throws an error:

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

My code is below:

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)

I would be grateful to know if there is anything wrong with my code.

Upvotes: 0

Views: 212

Answers (2)

Ukimiku
Ukimiku

Reputation: 618

This one returns a list:

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)

Upvotes: 1

Ukimiku
Ukimiku

Reputation: 618

Try this one:

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)

Upvotes: 0

Related Questions