知识点
四种条件语句格式
if 格式
if-else 格式
if-elif-else 格式
嵌套的条件语句格式
课后习题
1.接收用户输入的月收入 income 。
收入不超过 5000 元的部分,免
税。
超过 5000 元至 8000 元的部分
(含),税率 3%。
超过 8000 元至 17000 元的部分
(含),税率 10%。
超过 17000 元至 30000 元的部分
(含),税率 20%。
超过 30000 元的部分,税率 25%。
参考答案
income_str = input("请输入您的月收入: ")
income = float(income_str)
taxable_income = income - 5000 # 起征点
tax = 0.0
if taxable_income <= 0:
tax = 0.0
elif taxable_income <= 3000: # (8000 - 5000)
tax = taxable_income * 0.03
elif taxable_income <= 12000: # (17000 - 5000)
tax = (3000 * 0.03) + (taxable_income - 3000) * 0.10
elif taxable_income <= 25000: # (30000 - 5000)
tax = (3000 * 0.03) + (9000 * 0.10) + (taxable_income - 12000) * 0.20
else: # taxable_income > 25000
tax = (3000 * 0.03) + (9000 * 0.10) + (13000 * 0.20) + (taxable_income - 25000) * 0.25
# 确保税额不为负 (虽然理论上不会,但以防万一)
tax = max(0, tax)
print(f"您的月收入为: {income:.2f}")
print(f"应缴纳的个人所得税为: {tax:.2f}")
我的答案:
income = int(input("请输入你的月收入:"))
if income <= 5000:
r=0
print(f"你当前无需缴纳个人所得税")
elif income > 5000 and income < 8000:
r=(income - 5000) * 0.03
print(f"你当前需要纳税{r}元")
elif income > 8000 and income < 17000:
r=(8000 - 5000) * 0.03+(income - 8000) * 0.1
print(f"你当前需要纳税{r}元")
elif income >17000 and income < 30000:
r=(8000 - 5000) * 0.03+(17000 - 8000)*0.1+(income - 17000) * 0.2
print(f"你当前需要纳税{r}元")
else:
r=(8000 - 5000) * 0.03+(17000 - 8000)0.1+(30000 - 17000)0.2+(income - 3000) * 0.25
print(f"你当前需要纳税{r}元")
总结:
1.else 表达式错误 应该是 income - 30000,我写的3000
2.打印输出语句太多 ,不简洁
3.收入转化为浮点数优于整型
浙公网安备 33010602011771号