我嘗試使用兩種方法來解決這個問題。第一個是自下而上計算租金,而第二個是自上而下計算租金。不幸的是,我無法確定是什么導致它們回傳不同的輸出而不是相同的輸出。
問題是:從租房者的月收入中,房東可以保留第一個 $0 到 $9999 的 5% 剩余 $10k 到 $19999 的 10% 剩余 $20k 到 $49999 的 15% 剩余部分的 20% 5 萬美元至 99999 美元 剩余 100000 美元及以上的 30%。如果他們的月收入為 23,000 美元,請找出租戶本月必須支付的租金。
def calculate_rent_1(renter_salary):
rent = 0
if renter_salary > 0:
if renter_salary <= 9999:
return 0.05 * renter_salary
else:
rent = 0.05 * 9999
renter_salary -= 9999
if renter_salary > 9999:
if renter_salary <= 19999:
return rent 0.1 * renter_salary
else:
rent = 0.1 * 9999
renter_salary -= 9999
if renter_salary > 19999:
if renter_salary <= 49999:
return rent 0.15 * renter_salary
else:
rent = 0.15 * 29999
renter_salary -= 29999
if renter_salary > 49999:
if renter_salary <= 99999:
return rent 0.2 * renter_salary
else:
rent = 0.2 * 49999
renter_salary -= 49999
if renter_salary > 99999:
return rent 0.3 * renter_salary
def calculate_rent_2(renter_salary):
if not renter_salary: return 0
rent = 0
if 100000 <= renter_salary:
rent = 0.3 * (renter_salary - 100000)
renter_salary = 99999
if 50000 <= renter_salary:
rent = 0.2 * (renter_salary - 50000)
renter_salary = 49999
if 20000 <= renter_salary:
rent = 0.15 * (renter_salary - 20000)
renter_salary = 19999
if 10000 <= renter_salary:
rent = 0.1 * (renter_salary - 10000)
renter_salary = 9999
if 0 < renter_salary:
rent = 0.05 * renter_salary
return rent
print("Output from calculate_rent_1:", calculate_rent_1(23000)) # output: 1800.0500000000002
print("Output from calculate_rent_2:", calculate_rent_2(23000)) # output: 1949.8500000000001
uj5u.com熱心網友回復:
我想你意識到第二個功能是正確的。您的問題出在第一個函式中:
rent = 0
if renter_salary > 0:
if renter_salary <= 9999:
return 0.05 * renter_salary
else:
rent = 0.05 * 9999
renter_salary -= 10000
print( rent, renter_salary)
if renter_salary > 9999:
if renter_salary <= 19999: ##<<<<<<
return rent 0.1 * renter_salary
else:
rent = 0.1 * 9999
renter_salary -= 10000
在標記的行中,您正在檢查工資 <= 19999,但此時您已經從最初的 10,000 中減去。這需要檢查 9999。在其他子句中重復相同。
uj5u.com熱心網友回復:
這是我更正了我對calculate_rent_1 的方法并修改了calculate_rent_2 的上限后的答案。后者的原因是,如果renter_salary 為100,000 美元,我希望將高于99,999 美元的1 美元的0.3% 作為租金的一部分。
def calculate_rent_1(renter_salary):
if not renter_salary: return 0
rent = 0
if (renter_salary - 9999) <= 0:
return 0.05 * rent
rent = 0.05 * 9999
renter_salary -= 9999
if (renter_salary - 9999) <= 0:
return rent 0.1 * renter_salary
rent = 0.1 * 9999
renter_salary -= 9999
if (renter_salary - 29999) <= 0:
return rent 0.15 * renter_salary
rent = 0.15 * 29999
renter_salary -= 29999
if (renter_salary - 49999) <= 0:
return rent 0.2 * renter_salary
rent = 0.2 * 49999
renter_salary -= 49999
if renter_salary > 0:
return rent 0.3 * renter_salary
return rent
def calculate_rent_2(renter_salary):
if not renter_salary: return 0
rent = 0
if 100000 <= renter_salary:
rent = 0.3 * (renter_salary - 99999)
renter_salary = 99999
if 50000 <= renter_salary:
rent = 0.2 * (renter_salary - 49999)
renter_salary = 49999
if 20000 <= renter_salary:
rent = 0.15 * (renter_salary - 19999)
renter_salary = 19999
if 10000 <= renter_salary:
rent = 0.1 * (renter_salary - 9999)
renter_salary = 9999
if 0 <= renter_salary:
rent = 0.05 * renter_salary
return rent
print("Output from calculate_rent_1:", calculate_rent_1(23000)) # output: 1950.15
print("Output from calculate_rent_2:", calculate_rent_2(23000)) # output: 1950.1000000000001
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362780.html
上一篇:回圈、功能和組織方面的問題'
