Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need someone's opinion
#1
i am busy making my tax calculator global, the following is my code to calculate rebates, i want to use the min function and run a running balance.
it should check in what bracket the user's age is and then add the results to get the total rebate/benefit amount. i am still a beginner at coding and don't understand the concept 100%. the -1 is for if age is more than 75. and i am using a dictionary called rebate_table


rebate_table = {65: 17235, 75: 9444, -1: 3145}

def calculate_tax_benifits(age, rebate_table):
    total_tax_benefits = 0

    # Check if age is less than the minimum age threshold in rebate_table
    min_age = min(rebate_table.keys())
    if age < min_age:
        total_tax_benefits += rebate_table[min_age]
    else:
        # Iterate through each benefit data
        for benefit_age_threshold, benefit_amount in rebate_table.items():
            # Check if age is less than the current threshold
            if age < benefit_age_threshold:
                total_tax_benefits += benefit_amount
                break  # Break the loop, found the applicable rebate
            elif benefit_age_threshold == -1:  # Special case for age > 75
                total_tax_benefits += benefit_amount
            else:
                total_tax_benefits += benefit_amount

    print(total_tax_benefits)
    return total_tax_benefits
Gribouillis write May-16-2024, 12:27 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
In the fairly recent past your code wouldn't work at all because dictionaries were not ordered. Now you can loop through dictionary keys with confidence that they are in the same order they were added to the dictionary. But why use a dictionary when you don't look anything up? I would use a list or tuple.
rebate_table = ((65, 17235), (75, 9444), (76, 3145))


def rebate(age, table):
    """Return rebate for age."""
    for cutoff, rebate in table:
        if age < cutoff:
            break
    return rebate


for age in (-100, 64, 65, 66, 74, 75, 76, 100, 1000):
    print(age, "=", rebate(age, rebate_table))
This is called "binning". We determine which "bin" contains age and return the corresponding rebate. We have a default bin to catch all ages that don't fit into one of the other bins. I set the age for the default bin to 76. Since this is the last bin, any age greater than the age of the second to last bin will work. 75, 76, 1000, infinity all get the last rebate in the table,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Opinion: how should my scripts cache web download files? stevendaprano 0 776 Dec-17-2022, 12:19 AM
Last Post: stevendaprano
  Obligatory fish out of water -- seeking opinion on gettind started griffinxi 4 3,098 Jul-17-2018, 12:51 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020