gekko2670
gekko2670

Reputation: 53

Python raw input function for tax calculation

I am trying to make a simple calculator for working out the tax due on a salary. Please see the code below:

I keep getting this error and I don't know what is wrong, please help :) thanks!

Traceback (most recent call last):
  File "python", line 13
    elif salary > 11000 and salary < 43000:
       ^
SyntaxError: invalid syntax

CODE:

salary = raw_input ("What is your salary?")

print "So your gross annual salary is %r GBP" % (salary)
print "\nNow we need to calculate what your net salary is."

def taxes(salary):

    salary >= 0
    while true:
        if salary < 11000:
            tax = 0
    elif salary > 11000 and salary < 43000:
        tax = (0.2 * income) - 2200
    elif salary > 43000 and salary < 150000:
        tax = (0.4 * (salary - 43000)) + 6400
    elif salary > 150000:
        tax = ((salary - 150000) * 0.45) + 6400 + 42800
return tax

Upvotes: 1

Views: 5653

Answers (4)

Michael Platt
Michael Platt

Reputation: 1342

As the comments have already stated, your indentation is incorrect. See below:

def taxes(salary):

    salary >= 0
    tax = 0
    if salary < 11000:
        tax = 0
    elif salary > 11000 and salary < 43000:
        tax = (0.2 * income) - 2200
    elif salary > 43000 and salary < 150000:
        tax = (0.4 * (salary - 43000)) + 6400
    elif salary > 150000:
        tax = ((salary - 150000) * 0.45) + 6400 + 42800
    print("Value of tax is: " + str(tax))
    return tax

salary = raw_input ("What is your salary?")

print "So your gross annual salary is %r GBP" % (salary)
print "\nNow we need to calculate what your net salary is."
print("\nHere is your net salary after taxes: %r" % (taxes(int(salary))))

With python, indentations are how you tell the interpreter which blocks of code fall where (unlike with Java for example with semicolons being the end of line delimiter). By not indenting properly on your elif statements, you are essentially telling the program there is an elif without an if hence your syntax problem.

Upvotes: 0

khelili miliana
khelili miliana

Reputation: 3822

That because there are indent error in line number 12, now you can just copy pust this :

note : salary > 11000 and salary < 43000 equivalent to 11000 < salary < 43000 in python:

salary = raw_input ("What is your salary?")

print "So your gross annual salary is %r GBP" % (salary)
print "\nNow we need to calculate what your net salary is."

def taxes(salary):


    while true:
        if salary < 11000:
            tax = 0         
        elif 11000 < salary < 43000:
            tax = (0.2 * income) - 2200
        elif salary > 43000 and salary < 150000:
            tax = (0.4 * (salary - 43000)) + 6400
        elif salary > 150000:
            tax = ((salary - 150000) * 0.45) + 6400 + 42800
    return tax

Upvotes: 0

Andrew
Andrew

Reputation: 1082

It felt like there was a better way of doing this, so I came up with an alternative route:

tax_bands = [11000, 43000, 150000]
tax_amts = [0.2, 0.4, 0.45]
salary = 43001

Placing the thresholds and amounts into a list means that you can change them more easily if you need to.

The function below creates a list of the tax calculations, tax_list, and then a separate list of the maximum tax liability in each band called max_tax (the upper band has no maximum). It then compares the values in the lists, and overwrites the tax_list if the corresponding value is larger than the maximum. Then it calculates the sum of all values in tax_list greater than zero and returns it.

def taxes(salary, tax_bands, tax_amts):     

    tax_list = [(pct * (salary - band)) for (band, pct) in zip(tax_bands, tax_amts)] 
    max_tax = []
    for index, sal in enumerate(tax_bands[:-1]):
        max_tax.append(tax_bands[index + 1] - sal)
    max_tax = [segment * tax for segment, tax in zip(max_tax, tax_amts[:-1])]
    for index, value in enumerate(tax_list):
        try:
            if value > max_tax[index]:
                tax_list[index] = max_tax[index]
        except:
            pass
        tax_to_pay = sum([x for x in tax_list if x > 0])
    return tax_to_pay

print taxes(salary, tax_bands, tax_amts)

salary = input ("What is your salary?")

print "So your gross annual salary is %r GBP" % (salary)
print "\nYour net annual salary is: {} GBP".format(salary - taxes(salary, tax_bands, tax_amts))

To be super safe, you could also have the first line in the function call int(salary) using a try except just to check that it's the right type and that someone hasn't entered 43,000.

Upvotes: 0

Raheem Azeez Abiodun
Raheem Azeez Abiodun

Reputation: 36

Steps to correct your code

step1 : the salary data type should be of int, to correct..use the following code


step 2: Indentation is compulsory in python, so indent your code very well


step 3: Add an else statement after the conditional statements


step 4: indent return statement

change your code to this one

salary = int(raw_input ("What is your salary?"))

print "So your gross annual salary is %r GBP" % (salary)
print "\nNow we need to calculate what your net salary is."

def taxes(salary):

    salary >= 0
    while true:
        if salary < 11000:
            tax = 0
        elif salary > 11000 and salary < 43000:
            tax = (0.2 * income) - 2200
        elif salary > 43000 and salary < 150000:
            tax = (0.4 * (salary - 43000)) + 6400
        elif salary > 150000:
           tax = ((salary - 150000) * 0.45) + 6400 + 42800
        else :
            tax = undefined
    return tax

Upvotes: 1

Related Questions