大约一半的if语句不能正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大约一半的if语句不能正常工作相关的知识,希望对你有一定的参考价值。

我将使用基本运算符创建一个python程序来询问用户的收入,以便计算2017年累进税和2018年。输出应如下所示:

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

我现在的节目制作2017/2018的两个版画,但是会在2018年停止,如果是82501到157500的括号(嵌套在第4个elif中)...这是我现在的节目,因为它很久我会标出它停止工作的地方。

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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter your income as and integer with no commas: "))

    else:
        print("invalid")

我已经标记了不起作用的线。只是为了澄清,嵌套的if和打印产品在2017年和2018年之前产生结果,但是当收入在标记范围内且更大时,只会打印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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter 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("
Enter your income as and integer with no commas: "))

    else:
        print("-------------------")
答案

只需完成程序将要完成的任何陈述

income =  82502.0

将进入

elif income >= 37951 and income <= 91900:

但是对于2018年的计算,它不会满足其中的条件

    if income >= 38701 and income <= 82500:

因此它无法计算2018.它将无法达到您标记它的程度。

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

但是你确实有很好的测试,看你的程序没有用。边缘情况通常会产生问题。

以上是关于大约一半的if语句不能正常工作的主要内容,如果未能解决你的问题,请参考以下文章

编写代码片段的更简洁的方法

SQL Select 语句的用法

if语句中的ERRORLEVEL无法正常工作[关闭]

C语言-if语句问题

是否有在单个活动中处理多个片段的 Android 设计模式?

如何更改DataGridView行的背景颜色并在悬停时撤消?