-2

I need help I want the tax column to be calculated from the income column Also I need the information to be stored in the database from the user input.

import sqlite3

conn = sqlite3.connect('tax1.db')
c = conn.cursor()
#c.execute("""CREATE TABLE tax (
          #First text,
          #Last text,
          #Income real,
          #Tax real
          #)""")

#c.execute("INSERT INTO tax VALUES('Henry', 'Bass', 80000, 20000)")

for i in range(1):
         First = input("Please enter first name:")
         Last = input("Please enter last name:")
         Income = input("Please enter annual income:")
         conn.execute("INSERT INTO tax VALUES ('{}", "{}", 
         {})).format(First,Last,Income))

        c.execute("SELECT * FROM tax WHERE Last='Bass'")
        print(c.fetchall())
        conn.commit()
        conn.close()
2
  • So what are you having problems with from this code? Commented Aug 16, 2022 at 11:48
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Aug 16, 2022 at 12:15

1 Answer 1

0

To have the tax calculated from the income I would suggest something like a lookup table:

tax_ratios = {"low_income": 0.1, "medium_income": 0.2, "big_income": 0.3, "huge_income": 0.45}

Then after the user has made his inputs you could check with if-statements to which catogorie he belongs:

if Income > 250000:
    tax_ratio = tax_ratios["huge_income"]
if Income < 250000 and Income > 10000:
    tax_ratio = tax_ratios["big_income"]
if Income < 10000 and Income > 70000:
    tax_ratio = tax_ratios["medium_income"]
if Income < 70000:
    tax_ratio = tax_ratios["low_income"]

Then you can multiply the income with the tax ratio and store it in the database:

tax_absolute = tax_ratio*Income
conn.execute("INSERT INTO tax VALUES ('{}", "{}", 
         {}, {})).format(First,Last,Income, tax_absolute))

Of course this easy lookup table is not complex enough to deal with complicated tax systems but it might be a good start.

If I may add something regarding the database safety: With formatting the SQL-Query as a string it is easy for an attacker to perform what is called SQL-Injection. You find a good explanationhere.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much- I'm new to programming this isn't going to go live, I just couldn't figure out how to perform the following. I thought if I had someone explain it I could work backwards this has really helped.
No worries I would appreciate it if you could mark my answer as accepted :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.