繁体   English   中英

我的程序经过多个 if-else 语句后如何打印一组语句

[英]how do i print one set of statements after my program goes through multiple if-else statements

该程序出于税收目的要求用户收入,并根据输入的收入,根据一系列 if-else 语句计算所欠税额。 我的问题是,一旦超出 if-else 语句,是否可以在程序末尾只使用一组打印语句? 而不是将我的两个打印语句附加到每个 if else 语句?

taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income

# determine the amount of taxable income given three different tax rates
if income < 0:
    print("You cannot enter a negative value for the salary")

elif 0 <= income < 33389.01:
    taxrate = 0.108
    # calculate taxable income
    taxableIncome = income * taxRate
    # print tax value to two decimal places and print and tax bracket


elif 33389.01 <= income < 72164.01:
    taxrate = 0.1275
    taxableIncome = income * taxRate


else:  # income >= 72164.01
    taxrate = 0.174
    taxableIncome = income * taxRate

print("Employee's provincial tax value : CAD($) " + str(round(taxableIncome, 2)))
print("Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$0 .. $33389.01)")

这是最初的代码

taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income

# determine the amount of taxable income given three different tax rates
if income < 0:
    print("You cannot enter a negative value for the salary")


elif 0 <= income < 33389.01:
    taxrate = 0.108
    # calculate taxable income
    taxableIncome = income * 0.125
    # print tax value to two decimal places and print and tax bracket
    print("Employee's provincial tax value : CAD($) " + 
    str(round(taxableIncome, 2)))
    print("Employee's provincial tax bracket : " + str(.125 * 100) + "% [$0 
    .. $33389.01)")

elif 33389.01 <= income < 72164.01:
    taxrate = 0.1275
    taxableIncome = income * .1625
    print("Employee's provincial tax value : CAD($) " + 
    str(round(taxableIncome, 2)))
    print("Employee's provincial tax bracket : " + str(.1625 * 100) + "% [$0 
    .. $33389.01)")

else:  # income >= 72164.01
    taxrate = 0.174
    taxableIncome = income * .1775
    print("Employee's provincial tax value : CAD($) " + 
    str(round(taxableIncome, 2)))
    print("Employee's provincial tax bracket : " + str(.1775 * 100) + "% [$0 
    .. $33389.01)")

因此,在最后一组打印语句的情况下,由于程序开头的赋值语句,taxRate 保持在 0。 存储在 taxRate 中的值在 if-elif-else 语句中确实会更改一次,但在 if-elif-else 语句之外会返回 0 并打印 0。我想为唯一的一组 print 分配正确的 taxRate最后的陈述。

这就是你要找的东西

# get the users income
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())

taxRate = 0
# determine if negative income was entered
if income < 0:
    print("You cannot enter a negative value for the salary")
# determine appropriate tax rate to apply
else:
    if 0 <= income < 33389.01:
        taxRate = 0.108
        taxableIncome = income * taxRate

    elif 33389.01 <= income < 72164.01:
        taxRate = 0.1275
        taxableIncome = income * taxRate

    elif income >= 72164.01:
        taxRate = 0.174
        taxableIncome = income * taxRate
    # display tax rate and corresponding tax bracket
    print("Employee's provincial tax value : CAD($) " + str('{:.2f}'.format(taxableIncome)) + "\n" +
          "Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$72164.01 .. )")

尝试这个:

print(
"Employee's provincial tax value : CAD($) %s \n
 Employee's provincial tax bracket : %s % [$0 .. $33389.01"  % (str(round(taxableIncome, 2)), str(taxRate * 100))
)

您可以在行尾添加一个 \n 命令来添加新行,从而可能消除对两个语句的需要。

看起来像这样:

    taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income

# variable declarations for tax income 3 tax rates

# determine the amount of taxable income given three different tax rates
if income < 0:
    print("You cannot enter a negative value for the salary")

elif 0 <= income < 33389.01:
    taxrate = 0.108
    # calculate taxable income
    taxableIncome = income * taxRate
    # print tax value to two decimal places and print and tax bracket


elif 33389.01 <= income < 72164.01:
    taxrate = 0.1275
    taxableIncome = income * taxRate


else:  # income >= 72164.01
    taxrate = 0.174
    taxableIncome = income * taxRate

print("Employee's provincial tax value : CAD($) " + str(round(taxableIncome, 2,"\n","Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$0 .. $33389.01)")))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM