我是新來的蟒蛇。在MIT OCW 6.001計算機科學入門和Python編程中工作。需要創建一個腳本,用於計算撥出的部分以支付抵押貸款。我需要使用二分法來找到要放置的部分。當我運行我的腳本時,我得到了一堆關於我的函數「儲蓄」如何取0位置參數但生成1的生氣文本。我不知道這意味着什麼。請幫助!試圖計算抵押貸款的百分比儲蓄
下面的代碼:
"""
Created on Thu Mar 16 12:51:41 2017
@author: [redacted]
"""
num_guesses = int(0)
current_savings = int(0)
annual_salary = input('enter your annual salary:')
r = float(.04)
raise_pct = float (.07)
high = pct_saved = float(1.0)
low = 0
def savings():
total_savings = 0
num_months = 0
monthly_salary = int(annual_salary)/12
portion_saved = pct_saved*monthly_salary
for num_months in range(0,35):
num_months += 1
total_savings += (current_savings*r)/12 + portion_saved
if (num_months%6) == 0 :
monthly_salary += monthly_salary*raise_pct
portion_saved = monthly_salary*pct_saved
return total_savings
cost = 250000
epsilon = float(.001)
guess = (low + high)/2.0
if savings(high) < cost:
num_guesses += 1
print ('You cannot afford this house.')
while abs(savings(guess))-cost >= epsilon:
if savings(guess) < cost:
low = guess
else:
high = guess
guess = (high + low)/2.0
num_guesses += 1
print ('Number of guesses:', num_guesses)
print ('Savings percent is near:', pct_saved)
我建議你在解釋器中使用函數來掌握它們。 – Denziloe