【问题标题】:New to python and having trouble figuring out why the deductions are not including the tax in the totalpython 新手,无法弄清楚为什么扣除额不包括税款
【发布时间】:2021-12-26 09:52:06
【问题描述】:

我是编程新手,我正在尝试为我参加的 Python 基础课程编写一个程序。现在是第 1 天,我无法弄清楚为什么总扣除额不能计算出正确的金额。这是我写的代码:

    print("=================DEDUCTIONS=================")

print("SSS: " + sss_contribution)
print("PhilHealth: " + philhealth_contribution)
print("Other Loan: " + housing_loan)

tax_rate = .10
tax_total = int(gross_salary)*int(tax_rate)
print("Tax: " + str(tax_total))

total_deductions = int(sss_contribution) + int(philhealth_contribution) + int(housing_loan) + int(tax_total)
print("Total Deductions: " + str(total_deductions))

net_salary = float(gross_salary) - float(total_deductions)

print("NET SALARY: " + str(net_salary))

我得到了正确的 NET SALARY 金额,但总扣除额仅反映 SSS、PhilHealth 和 Housing 的总和。谢谢。

【问题讨论】:

    标签: python addition


    【解决方案1】:

    您将 tax_rate 声明为浮点数,因此请尝试:

    tax_total = int(gross_salary)*float(tax_rate)
    

    如果您将 tax_rate 声明为 5.10,则执行 int(tax_rate) 会返回 5。而执行 float(tax_rate) 会返回 5.1。

    在您的示例中,您将 tax_rate 声明为 0.10,因此您的 tax_total 变为 0,因为 int(tax_rate) 为 0。这就是为什么您的税不包含在您的计算中

        print("=================DEDUCTIONS=================")
    
    sss_contribution = 500
    philhealth_contribution = 600
    housing_loan = 500.20
    gross_salary = 2000
    
    print("SSS: " +str(sss_contribution))
    print("PhilHealth: " +str(philhealth_contribution))
    print("Other Loan: " +str(housing_loan))
    
    tax_rate = .10
    tax_total = int(gross_salary)*float(tax_rate)
    print("Tax: " + str(tax_total))
    
    total_deductions = int(sss_contribution) + int(philhealth_contribution) + float(housing_loan) + int(tax_total)
    print("Total Deductions: " + str(total_deductions))
    
    net_salary = float(gross_salary) - float(total_deductions)
    
    print("NET SALARY: " + str(net_salary))
    

    【讨论】:

    • 您好,感谢您的回复。我现在尝试使用 float 和其他变体,它仍然没有反映出来。目前这就是我所拥有的:print("SSS: " + sss_contribution) print("PhilHealth: " + philhealth_contribution) print("Other Loan: " + housing_loan) tax_rate = float(.10) tax_total = float(gross_salary)*float(tax_rate) print("Tax: " + float(tax_total)) total_deductions = float(sss_contribution) + float(philhealth_contribution) + float(housing_loan) + float(tax_total) print("Total Deductions: " + str(total_deductions))
    • 我用您的代码工作所需的更改更新了我的答案。
    猜你喜欢
    • 2011-07-19
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多