0
私は現在、自分でPythonを学習しています。これは二等分検索を含む問題の最後の問題です。私はこの問題を解決するのに非常に近いと感じていますが、どちらの部分が間違っているのかはわかりません。36ヶ月で住宅着払いの貯蓄率を計算
問題:
Write a program to calculate the savings percentage you need each month to afford
the down payment in three years (36 months).
Down payment: $250000
Semi-annual raise: 0.07 (7% raise every 6 months)
Investment return: 0.04 (4%)
コード:
salary = 150000
semi_annual_raise = 0.07
investment_return = 0.04
down_payment = 250000
low = 0
high = 10000
percent_saved = int((low + high)/2)
current_savings = 0.0
steps = 0
months = 0
print('Annual salary:', salary)
while current_savings < down_payment:
percent_saved = percent_saved/10000
monthly_salary = salary/12
current_savings += current_savings*investment_return/12
current_savings += monthly_salary*percent_saved
if current_savings < down_payment:
low = percent_saved
elif current_savings > down_payment:
high = percent_saved
print('Best savings rate:', percent_saved)
print('Steps in bisection search:', steps)
break
else:
print('It is not possible to pay the down payment in three years.')
break
percent_saved = (low + high)/2
steps += 1
months += 1
マイ出力:
Annual salary: 150000
Best savings rate: 0.5000250012500626
Steps in bisection search: 39
テストケース(正しい出力):
Annual salary: 150000
Best savings rate: 0.4411
Steps in bisection search: 12
誰かが私が間違っていた箇所を指摘できればそれは大変感謝しています。私は答えを受け取るのではなく、問題を解決する方法を知りたい。ありがとうございました。
実際に出力されているものと期待される出力を表示してください。そして、あなたはまだどんなデバッグもやっていますか? – Carcigenicate
あなたはあなたのwhileループで立ち往生しています – thesonyman101
$ 150Kのベースで6ヶ月ごとに7%の引き上げがありますか?あなたの仕事はできますか? ;-) – ShadowRanger