用于税收计算的Python原始输入函数

时间:2016-08-23 15:43:14

标签: python

我正在尝试制作一个简单的计算器来计算工资税。请参阅以下代码:

我一直收到这个错误,我不知道出了什么问题,请帮忙:)谢谢!

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

4 个答案:

答案 0 :(得分:1)

纠正代码的步骤

步骤1:工资数据类型应为int,以便更正..使用以下代码

第2步:在python中必须使用缩进,因此要非常好地缩进代码

步骤3:在条件语句

之后添加else语句

第4步:缩进返回语句

将您的代码更改为此

.submit_button

答案 1 :(得分:0)

正如评论已经说明的那样,你的缩进是不正确的。见下文:

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

使用python,缩进就是告诉解释器哪些代码块落在哪里(与Java不同,例如用分号作为行分隔符的结尾)。如果没有正确缩进elif语句,您实际上是在告诉程序elif没有if因此您的语法问题。

答案 2 :(得分:0)

因为行号12中有缩进错误,现在你可以复制pust:

注意:salary > 11000 and salary < 43000相当于python中的11000 < salary < 43000

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

答案 3 :(得分:0)

感觉有更好的方法可以做到这一点,所以我提出了另一条路线:

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

将阈值和金额放入列表意味着您可以根据需要更轻松地更改它们。

下面的函数会创建税收计算列表tax_list,然后单独列出每个名为max_tax的频段中的最高纳税义务(上限没有最大值)。 然后,它会比较列表中的值,如果相应的值大于最大值,则覆盖tax_list。 然后它计算tax_list中大于零的所有值的总和并返回它。

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

为了超级安全,您还可以使用int(salary)在函数调用try except中找到第一行,只是为了检查它是否是正确的类型而有人没有输入43,000 }。