繁体   English   中英

公式 Python 中的负数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM