大约一半的if语句无法正常工作

时间:2018-10-04 02:18:19

标签: python python-3.x if-statement printing

我要使用基本运算符制作一个python程序,向用户索要收入,以便与2017年一起计算2017年的累进税。输出应如下所示:

Income: 15000
2017 tax: (tax amount here) #will skip the math for samples
2018 tax: (tax amount here)

截至目前,我的程序会同时打印2017/2018年的两张照片,但如果括号为82501至157500(嵌套在第4个elif中),则将在2018年的嵌套时停止。...这是我的程序,因为它很长我会指出它在哪里停止工作。

income = float(input("Enter your income to calculate tax: "))

#Loop input/calculations
while income > 0:
    print("----------------------------------------------")
    if income >= 0 and income <= 9325:
        bracket1 = income * 0.10
        tax2017 = bracket1
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 0 and income <= 9525:
            newbracket1 = income * 0.10
            tax2018 = newbracket1
            print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 9326 and income <= 37950:
        bracket1 = 9325 * 0.10
        bracket2 = (income - 9326) * 0.15
        tax2017 = bracket1 + bracket2
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 9526 and income <=38700:
            newbracket1 = 9526 * 0.10
            newbracket2 = (income - 9525) * 0.12
            tax2018 = newbracket1 + newbracket2
            print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 37951 and income <= 91900:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (income - 37951) * 0.25
        tax2017 = bracket1 + bracket2 + bracket3
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 38701 and income <= 82500:
            newbracket1 = 9526 * 0.10
            newbracket2 = (38700 - 9526) * 0.12
            newbracket3 = (income - 38700) * 0.22
            tax2018 = newbracket1 + newbracket2 + newbracket3
            print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 91901 and income <= 191650:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (income - 91901) * 0.28
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 82501 and income <= 157500: #HERE STOPS WORKING, the 2018 from here on doesn't print
            newbracket1 = 9526 * 0.10
            newbracket2 = (38700 - 9526) * 0.12
            newbracket3 = (82500 - 38701) * 0.22
            newbracket4 = (income - 82500) * 0.24
            tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4
            print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 191651 and income <= 416700:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (income - 191651) * 0.33
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 157501 and income <= 200000:
            newbracket1 = 9526 * 0.10
            newbracket2 = (38700 - 9526) * 0.12
            newbracket3 = (82500 - 38701) * 0.22
            newbracket4 = (157500 - 82501) * 0.24
            newbracket5 = (income - 157500) * 0.32
            tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5
            print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 416701 and income <= 418400:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (416700 - 191650) * 0.33
        bracket6 = (income - 416701) * 0.35
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
        if income >= 200001 and income <= 500000:
            newbracket1 = 9526 * 0.10
            newbracket2 = (38700 - 9526) * 0.12
            newbracket3 = (82500 - 38701) * 0.22
            newbracket4 = (157500 - 82501) * 0.24
            newbracket5 = (200000 - 157501) * 0.32
            newbracket6 = (income - 200001) * 0.35
            tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6
            print("2018 tax: ",tax2018)
        income = float(input("\nEnter your income as and integer with no commas: "))
    elif income >= 418401:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (416700 - 191650) * 0.33
        bracket6 = (418400 - 416700) * 0.35
        bracket7 = (income - 418401) * 0.396
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6 + bracket7
        print("Income: ",income)
        print("2017 tax: ",tax2017)
        income = float(input("\nEnter your income as and integer with no commas: "))

    else:
        print("invalid")

我已将行标记为无效。为了澄清起见,在输出2017和2018年结果之前,嵌套的if和prints,但是当收入在标记的范围内且更大时,仅将打印2017的税款。

我的收入为82502及以上的结果是:

Enter your income to calculate tax: 82502
----------------------------------------------
Income:  82502.0
2017 tax:  16364.00

Enter your income as and integer with no commas: 

已解决: 感谢您的所有评论和答案,他们为我整理了一些东西! 这是我重做的,似乎可以正常工作,所以我很满意。我将再添加一些计算,希望也能很好:)

income = float(input("Enter your income to calculate tax: "))

#Loop input/calculations
while income > 0:
    if income >= 0 and income <= 9325:
        bracket1 = income * 0.10
        tax2017 = bracket1
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 0 and income <= 9525:
        newbracket1 = income * 0.10
        tax2018 = newbracket1
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 9326 and income <= 37950:
        bracket1 = 9325 * 0.10
        bracket2 = (income - 9326) * 0.15
        tax2017 = bracket1 + bracket2
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 9526 and income <=38700:
        newbracket1 = 9526 * 0.10
        newbracket2 = (income - 9525) * 0.12
        tax2018 = newbracket1 + newbracket2
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 37951 and income <= 91900:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (income - 37951) * 0.25
        tax2017 = bracket1 + bracket2 + bracket3
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 38701 and income <= 82500:
        newbracket1 = 9526 * 0.10
        newbracket2 = (38700 - 9526) * 0.12
        newbracket3 = (income - 38700) * 0.22
        tax2018 = newbracket1 + newbracket2 + newbracket3
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 91901 and income <= 191650:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (income - 91901) * 0.28
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 82501 and income <= 157500:
        newbracket1 = 9526 * 0.10
        newbracket2 = (38700 - 9526) * 0.12
        newbracket3 = (82500 - 38701) * 0.22
        newbracket4 = (income - 82500) * 0.24
        tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 191651 and income <= 416700:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (income - 191651) * 0.33
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 157501 and income <= 200000:
        newbracket1 = 9526 * 0.10
        newbracket2 = (38700 - 9526) * 0.12
        newbracket3 = (82500 - 38701) * 0.22
        newbracket4 = (157500 - 82501) * 0.24
        newbracket5 = (income - 157500) * 0.32
        tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 416701 and income <= 418400:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (416700 - 191650) * 0.33
        bracket6 = (income - 416701) * 0.35
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 200001 and income < 500000:
        newbracket1 = 9526 * 0.10
        newbracket2 = (38700 - 9526) * 0.12
        newbracket3 = (82500 - 38701) * 0.22
        newbracket4 = (157500 - 82501) * 0.24
        newbracket5 = (200000 - 157501) * 0.32
        newbracket6 = (income - 200001) * 0.35
        tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))
    if income >= 418401:
        bracket1 = 9325 * 0.10
        bracket2 = (37950 - 9325) * 0.15
        bracket3 = (91901 - 37950) * 0.25
        bracket4 = (191650 - 91901) * 0.28
        bracket5 = (416700 - 191650) * 0.33
        bracket6 = (418400 - 416700) * 0.35
        bracket7 = (income - 418401) * 0.396
        tax2017 = bracket1 + bracket2 + bracket3 + bracket4 + bracket5 + bracket6 + bracket7
        print("Income: ",income)
        print("2017 tax: ",format(tax2017,'.2f'))
    if income >= 500000:
        newbracket1 = 9526 * 0.10
        newbracket2 = (38700 - 9526) * 0.12
        newbracket3 = (82500 - 38701) * 0.22
        newbracket4 = (157500 - 82501) * 0.24
        newbracket5 = (200000 - 157501) * 0.32
        newbracket6 = (500000 - 200001) * 0.35
        newbracket7 = (income - 500000) * 0.37
        tax2018 = newbracket1 + newbracket2 + newbracket3 + newbracket4 + newbracket5 + newbracket6 + newbracket7
        print("2018 tax: ",format(tax2018,'.2f'))
        income = float(input("\nEnter your income as and integer with no commas: "))

    else:
        print("-------------------")

1 个答案:

答案 0 :(得分:1)

只需处理程序将要执行的语句

income =  82502.0

将进入

elif income >= 37951 and income <= 91900:

但是对于2018年的计算,它将无法满足其中的条件

    if income >= 38701 and income <= 82500:

因此它无法计算2018。它不会达到您标记它的地步。

2017年的计算应独立于2018年的计算。在很多情况下,您的计算将不会打印2018年税款。

但是您确实进行了良好的测试,以查看您的程序无法正常工作。边缘情况通常会带来问题。