Tax Lab Help me solve this test!

This is the question:
Country X calculates tax for its citizens using a graduated scale rate as shown below:

Yearly Income: 0 - 1000
Tax Rate: 0%

Yearly Income: 1,001 - 10,000
Tax Rate: 10%

Yearly Income: 10,001 - 20,200
Tax Rate: 15%

Yearly Income: 20,201 - 30,750
Tax Rate: 20%

Yearly Income: 30,751 - 50,000
Tax Rate: 25%

Yearly Income: Over 50,000
Tax Rate: 30%

Write a Python function named calculate_tax that will take as an argument, a dictionary containing key-value pairs of people’s names as the keys and their yearly incomes as the values.

The function should return a dictionary containing key-value pairs of the same people’s names as keys and their yearly tax bill as the values. For example, given the sample input below:

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

The output would be as follows:

{
‘Alex’: 0,
‘James’: 2490,
‘Kinuthia’: 15352.5
}

The tax for James would be calculated as follows:

The first 1000 (1000 - 0)
Calculation: 1,000 * 0%
Tax: 0

The next 9000 (10,000 - 1,000)
Calculation: 9,000 * 10%
Tax: 900

The next 10,200 (20,200 -10,000)
Calculation: 10,200 * 15%
Tax: 1530

The remaining 300 (20,500 - 20,200)
Calculation: 300 * 20%
Tax: 60

Total Income: 20,500
Total Tax: 0 + 900 + 1530 + 60 = 2490

THE TEST
from unittest import TestCase

class CalculateTaxTests(TestCase):
def test_it_calculates_tax_for_one_person(self):
result = calculate_tax({“James”: 20500})
self.assertEqual(result, {“James”: 2490.0}, msg=“Should return {‘James’: 2490.0} for the input {‘James’: 20500}”)

def test_it_calculates_tax_for_several_people(self):
income_input = {“James”: 20500, “Mary”: 500, “Evan”: 70000}
result = calculate_tax(income_input)
self.assertEqual({“James”: 2490.0, “Mary”: 0, “Evan”: 15352.5}, result,
msg=“Should return {} for the input {}”.format(
{“James”: 2490.0, “Mary”: 0, “Evan”: 15352.5},
{“James”: 20500, “Mary”: 500, “Evan”: 70000}
)
)

def test_it_does_not_accept_integers(self):
with self.assertRaises(ValueError) as context:
calculate_tax(1)
self.assertEqual(
“The provided input is not a dictionary.”,
context.exception.message, “Invalid input of type int not allowed”
)

def test_calculated_tax_is_a_float(self):
result = calculate_tax({“Jane”: 20500})
self.assertIsInstance(
calculate_tax({“Jane”: 20500}), dict, msg=“Should return a result of data type dict”)
self.assertIsInstance(result[“Jane”], float, msg=“Tax returned should be an float.”)

def test_it_returns_zero_tax_for_income_less_than_1000(self):
result = calculate_tax({“Jake”: 100})
self.assertEqual(result, {“Jake”: 0}, msg=“Should return zero tax for incomes less than 1000”)

def test_it_throws_an_error_if_any_of_the_inputs_is_non_numeric(self):
with self.assertRaises(ValueError, msg=‘Allow only numeric input’):
calculate_tax({“James”: 2490.0, “Kiura”: ‘200’, “Kinuthia”: 15352.5})

def test_it_return_an_empty_dict_for_an_empty_dict_input(self):
result = calculate_tax({})
self.assertEqual(result, {}, msg=‘Should return an empty dict if the input was an empty dict’)

And my code:
income_input = {“Alex”: 500, “James”: 20500, “Kinuthia”: 70000}

def calculate_tax(income_input):
for item in income_input:
income = income_input[item]
# print(income)

    if (income >= 0) and (income <= 1000):
        tax = (0*income)

    elif (income > 1000) and (income <= 10000):
        tax = (0.1 * (income-1000))

    elif (income > 10000) and (income <= 20200):
        tax = ((0.1*(10000-1000)) + (0.15*(income-10000)))

    elif (income > 20200) and (income <= 30750):
        tax = ((0.1*(10000-1000)) + (0.15*(20200-10000)) + (0.2*(income-20200)))

    elif (income > 30750) and (income <= 50000):
        tax = ((0.1*(10000-1000)) + (0.15*(20200-10000)) + (0.2*(30750-20200)) + (0.25*(income-30750)))

    elif (income > 50000):
        tax = ((0.1*(10000-1000)) + (0.15*(20200-10000)) + (0.2*(30750-20200)) + (0.25*(50000-30750)) + (0.3*(income-50000)))   
    else:
        pass
    income_input[item] = int(tax)
return income_input

I keep getting this error:

help!

When is this lab due? I don’t wish to post working code until after the deadline. Then we can compare notes.

  1. income_input[item] = int(tax) The output is a float and should be left as one.

  2. Lots of repetition in the code. A simpler approach is warranted.

  3. income_input should not be the name of the parameter AND the global dictionary. Use something different.

  4. return income_input per above, wrong variable, but also note that an empty dict should be declared locally, and then the completed dictionary returned.

I have a solution that with indents included is less than 600 bytes, so we know it can be done with less bloat.

Alex 0
Kinuthia 15352.5
James 2490.0

A post was split to a new topic: Object oriented Lab

Lab was due on 10th of September so it’s safe to say I’m out, could you share so that I can see where I was going wrong?

Create a function called binary_converter. Inside the function, implement an algorithm to convert decimal numbers between 0 and 255 to their binary equivalents.

For any invalid input, return string Invalid input

Example: For number 5 return string 101

this should work, thats the code i wrote for mine.
def binary_converter(x):
if not (isinstance(x, int) and (x >= 0 and x <= 255)):
print (“Invalid input”)
return “Invalid input”
converted = str(bin(x)[2:])
return converted

error/bug in ur code

pls help me on how to code am doing my assessment with andela and this code are baffling me too much thank u.

The code I posted works…may be u didn’t indent it well. What error are u
getting.

To anyone planning to help out here:

For these types of questions, homework, labs, etc., help the user by explaining how Python works rather than dumping a load of code. You might need to ask them to explain their thinking so you can give a targeted response.

Code without explanation may be removed without further discussion.

1 Like

oh ok indent means pls.

mine seem to be getting an error still can yo help ?