我是Python中的新編程。我正在嘗試創建一個只支付最低付款(從10開始)的功能,它只是返回餘額(付款後12個月)。我的循環沒有調用我的函數
在函數的外面,我使用了一個調用該函數的循環,並檢查餘下的餘額是零還是小於零。如果不是,則增加最低支付額+ 10美元並再次調用該功能。當餘額爲零或小於零時,打印出最低的付款。
從理論上講,輸出應爲如下:
測試案例1:
balance = 3329
annualInterestRate = 0.2
Result Your Code Should Generate:
******Lowest Payment: 310******
Test Case 2:
balance = 4773
annualInterestRate = 0.2
Result Your Code Should Generate:
**最低付款方式:440 * *
Test Case 3:
balance = 3926
annualInterestRate = 0.2
Result Your Code Should Generate:
**最低付款方式:360 * *
到目前爲止,這是我所:
balance = 100
annualInterestRate = 0.2
per_month = (annualInterestRate/12)
# Answer is 0.0166666 but I need it to be 0.01, so figured out to convert to string then to float, not the most elegant , but practical. :)
convert_to_str = str(per_month)[:4]
per_month = float(convert_to_str)
lowest_payment = 0
def main():
i = 0
while i < 11:
global balance
global lowest_payment
global per_month
balance = balance - lowest_payment
balance = ((balance * per_month) + balance)
i = i +1
#print (balance)
main()
if balance <= 0 or balance == 0:
print "Lowest Payment: " + str(lowest_payment)
else:
lowest_payment = lowest_payment + 10
main()
的問題是,是不是我的執行功能,再看一遍循環。我曾嘗試過使用if和while循環。在我的while循環下面,給出一個無限循環:
while balance >= 0 or balance != 0:
lowest_payment = lowest_payment + 10
main()
if balance <= 0:
print "Lowest Payment: " + str(lowest_payment)
您的幫助是非常感謝。
什麼是預期產出? – Christian
從理論上講, 餘額= 3329 annualInterestRate = 0.2 代碼應該生成: 最低付款:310 測試案例2: 餘額= 4773 annualInterestRate = 0.2 應該產生: 最低付款:440 測試案例3: 餘額= 3926 annualInterestRate = 0.2 最低支付:360 –
您確定使用的公式是正確的嗎? – Christian