python - 我的程序经过多个 if-else 语句后如何打印一组语句
问题描述
该程序出于税收目的要求用户收入,并根据输入的收入,根据一系列 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 .. )")
推荐阅读
- javascript - 当我尝试测试运行我的 MySQL 数据库时收到此错误消息
- gpio - 覆盆子 pi4 不适用于 GPIO 3.3V 引脚
- node.js - 使用 mongo 固定装置进行 Mocha 测试向我抛出了超过 2000 毫秒的超时错误
- python - 绘制奇数个子图
- docker - 使用带有 docker login 的 Jfrog 访问令牌
- javascript - 在分隔字符串中,仅当子字符串与替换值匹配时,才替换分隔符之间的子字符串
- typescript - nuxt-i18n:Lang Switcher 错误“对象可能是‘未定义’
- javascript - 更新嵌套对象数组中的属性
- java - 如果值不适合,则设置字节
- python - Python - 检测字符串中重音 HTML 转义的最佳方法?