在下面的代码示例中,我希望使deductions
等于4个值中的一个,具体取决于用户输入的输入(查看if else语句)。这可能吗?你知道吗
deductions1 = 0.12 + 0.04 + 0.01 + 0.06
deductions2 = 0.20 + 0.07 + 0.03 + 0.06
deductions3 = 0.30 + 0.09 + 0.05 + 0.06
deductions4 = 0.38 + 0.11 + 0.07 + 0.06
deductions = monthly_salary = hours_worked * hourly_pay
if monthly_salary < 4000:
deduction_rate = deductions1
elif monthly_salary >= 4000 and monthly_salary < 8000:
deduction_rate = deductions2
elif monthly_salary >= 8000 and monthly_salary < 16000:
deduction_rate = deductions3
else:
deductions = deductions4
net_salary = monthly_salary - (deductions * monthly_salary)
这可能就是你想做的吗?地址:
您可以简化它,您不需要elifs的下限-如果早一点匹配elsewise。你知道吗
输入:
343
(h)和12
(pay/h),输出:通过将其分解成一个方法,您可以将决策过程从主代码中封装出来,并使其“更精简”。具体细节在您的函数中清晰可见。你知道吗
相关问题 更多 >
编程相关推荐