python 贷款计算器在运行时出现计算错误
Calculation error at runtime in python loan calculator
我正在尝试构建一个贷款计算器,该计算器计算每月的欠款和利率以及开始时的首付和每月分期付款,以便它可以输出您需要多少个月还清你的债务。当我输入低于 10% 的 interest_rate 时,该程序工作正常,但如果我输入任何数字,例如 18%,它就会冻结并且没有输出。当我停止 运行 程序时,它给我这些错误:
Traceback (most recent call last):
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 35, in <module>
months_to_finish = get_months(price) # this returns the number of months from the counter var
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 6, in get_months
price = price - installments
KeyboardInterrupt
这是我的代码:
def get_months(price):
counter = 0
price = price - down_payment
while price > 0:
price = price + price * interest_rate
price = price - installments
counter += 1
return counter
if __name__ == '__main__':
price = float(input("Enter price here: "))
interest_rate = float(input("Enter interest rate here: %")) / 100
while interest_rate < 0:
interest_rate = float(input('Invalid interest rate please try again: %')) / 100
down_payment = int(input("Enter your down payment here: $"))
while down_payment > price or down_payment < 0:
down_payment = int(input('Invalid down payment please try again: $'))
choice = input("Decision based on Installments (i) or Months to finish (m), please write i or m: ").lower()
if choice == 'm':
print('m')
elif choice == 'i':
installments = int(input("What's your monthly installment budget: ")) # get the installments
months_to_finish = get_months(price) # this returns the number of months from the counter var
print(f"It will take {months_to_finish} months to finish your purchase.")
else:
print('Invalid choice program ended')
这些是测试值:
在此输入价格:22500
在此输入利率:%18
在这里输入您的预付款:$0
根据分期付款 (i) 或完成月数 (m) 决定,请写 i 或 m: i
您每月的分期付款预算是多少:3000
初始本金 22,500 美元,利率 18%,首付 0 美元,每月还款 3,000 美元,需要无数个月才能还清贷款。在利率为 18% 的一个时期后,您已经累积了 4,050 美元的利息。由于您每期只需支付 3,000 美元,您甚至没有支付新的利息金额,而且您欠下的总金额将永远增长。您可能想在某个地方检查每月付款是否大于第一个月的利息。您可以这样修改您的代码:
if installments < (price - down_payment) * interest_rate:
print("The purchase cannot be made with these amounts.")
else:
months_to_finish = get_months(price)
print(f"It will take {months_to_finish} months to finish your purchase.")
l=[]
n=int(input())
i=0
for i in range(n):
l.append(str(input()))
def long(x):
if x>10:
return print(l[i][0]+str(len(l[i])-2)+l[i][-1])
def short(x):
if x<=10:
return print(l[i])
i=0
for i in range(len(l[i])):
long(len(l[i]))
short(len(l[i]))
continue
我正在尝试构建一个贷款计算器,该计算器计算每月的欠款和利率以及开始时的首付和每月分期付款,以便它可以输出您需要多少个月还清你的债务。当我输入低于 10% 的 interest_rate 时,该程序工作正常,但如果我输入任何数字,例如 18%,它就会冻结并且没有输出。当我停止 运行 程序时,它给我这些错误:
Traceback (most recent call last):
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 35, in <module>
months_to_finish = get_months(price) # this returns the number of months from the counter var
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 6, in get_months
price = price - installments
KeyboardInterrupt
这是我的代码:
def get_months(price):
counter = 0
price = price - down_payment
while price > 0:
price = price + price * interest_rate
price = price - installments
counter += 1
return counter
if __name__ == '__main__':
price = float(input("Enter price here: "))
interest_rate = float(input("Enter interest rate here: %")) / 100
while interest_rate < 0:
interest_rate = float(input('Invalid interest rate please try again: %')) / 100
down_payment = int(input("Enter your down payment here: $"))
while down_payment > price or down_payment < 0:
down_payment = int(input('Invalid down payment please try again: $'))
choice = input("Decision based on Installments (i) or Months to finish (m), please write i or m: ").lower()
if choice == 'm':
print('m')
elif choice == 'i':
installments = int(input("What's your monthly installment budget: ")) # get the installments
months_to_finish = get_months(price) # this returns the number of months from the counter var
print(f"It will take {months_to_finish} months to finish your purchase.")
else:
print('Invalid choice program ended')
这些是测试值:
在此输入价格:22500
在此输入利率:%18
在这里输入您的预付款:$0
根据分期付款 (i) 或完成月数 (m) 决定,请写 i 或 m: i
您每月的分期付款预算是多少:3000
初始本金 22,500 美元,利率 18%,首付 0 美元,每月还款 3,000 美元,需要无数个月才能还清贷款。在利率为 18% 的一个时期后,您已经累积了 4,050 美元的利息。由于您每期只需支付 3,000 美元,您甚至没有支付新的利息金额,而且您欠下的总金额将永远增长。您可能想在某个地方检查每月付款是否大于第一个月的利息。您可以这样修改您的代码:
if installments < (price - down_payment) * interest_rate:
print("The purchase cannot be made with these amounts.")
else:
months_to_finish = get_months(price)
print(f"It will take {months_to_finish} months to finish your purchase.")
l=[]
n=int(input())
i=0
for i in range(n):
l.append(str(input()))
def long(x):
if x>10:
return print(l[i][0]+str(len(l[i])-2)+l[i][-1])
def short(x):
if x<=10:
return print(l[i])
i=0
for i in range(len(l[i])):
long(len(l[i]))
short(len(l[i]))
continue