[英]Negative number in formula Python
所以我正在為我的計算機科學課編寫一個貸款計算器程序,但我被卡住了。 在計算總支付金額的公式中,它有一個負數。 我不知道如何修復它。 我仍然需要添加一個 while 循環,這樣程序還沒有完成。 但到目前為止,這是我的程序:
import math
# Returns the number of payments per year
def get_payments_per_year(frequency):
if frequency == "a":
return 1
elif frequency == "s":
return 2
elif frequency == "q":
return 4
elif frequency == "m":
return 12
elif frequency == "d":
return 365
# Loan calculation functions removed; don't get called after the error.
# main
another = "Y"
amount = int(input("Enter amount of loan: "))
annual_ir = int(input("Enter annual interest rate: "))
payoff_yrs = int(input("Enter payoff period in years: "))
print("How frequently will you pay?")
frequency = input("[a]nnually \ [s]emi-annually \ [q]uarterly \ [m]onthly \ [d]aily? ")
payment_count = get_payments_per_year(frequency)
dec_annual_ir = annual_ir / 100
per_int_rate = dec_annual_ir / payment_count
total_pay_count = frequency * payoff_yrs
total_pay = (per_int_rate * amount) / (1 - (math.pow((1 + per_int_rate),-total_pay_count)))
# Code after this point isn't executed.
它返回此錯誤:
Traceback (most recent call last):
line 81, in <module>
total_pay = (per_int_rate * amount) / (1 - (math.pow((1 + per_int_rate),-total_pay_count)))
TypeError: bad operand type for unary -: 'str'
是的,您在-total_pay_count
有非法操作。 看數據流:
payoff_yrs = int(input("Enter payoff period in years: "))
frequency = input("[a]nnually \ [s]emi-annually \ [q]uarterly \ [m]onthly \ [d]aily? ")
total_pay_count = frequency * payoff_yrs
total_pay_count
是一個字符串。 對於我 30 年的每月貸款,它是一串 30 米。 我想你想要
payment_count = get_payments_per_year(frequency)
total_pay_count = payment_count * payoff_yrs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.