Python 在程序执行多个if-else语句后,如何打印一组语句

Python 在程序执行多个if-else语句后,如何打印一组语句,python,if-statement,Python,If Statement,程序出于税务目的要求用户收入,并根据输入的收入,根据一系列if-else语句计算所欠税款。我的问题是,一旦退出if-else语句,是否有可能在程序的末尾只有一组print语句?而不是将我的两个print语句附加到每个if-else语句 taxRate = 0; print("Please enter employee's annual taxable income ... CAD($) ") income = eval(input()) # get the users inc

程序出于税务目的要求用户收入,并根据输入的收入,根据一系列if-else语句计算所欠税款。我的问题是,一旦退出if-else语句,是否有可能在程序的末尾只有一组print语句?而不是将我的两个print语句附加到每个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)")
因此,对于末尾的一组print语句,由于程序开头的assignment语句,taxRate保持在0。存储在taxRate中的值在if-elif-else语句内部会更改一次,但在if-elif-else语句外部会返回0一次,并打印0。我想为末尾的一组也是唯一一组打印语句分配正确的税率。

尝试以下操作:

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

这就是你要找的

# 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 .. )")

你写的代码有什么问题吗?如果elif else Station你发布的代码看起来完全符合你的要求,讲师只希望在最后有一组打印语句,而不是在每个语句上附加多个副本。因此,你的问题一点也不清楚。在我们进一步讨论之前,你能修正一下缩进吗?重复类似或相同的内容是不好的做法!密码你的老师是对的。