Python Loan表整数错误

时间:2018-02-17 20:39:36

标签: python-3.x

Ok I'm having alot of trouble with this code because I can't get the table to work properly. I'm getting an error code that reads :
    Traceback (most recent call last):
  File "C:\Users\User\Documents\python\project1.py", line 71, in <module>
    main()
  File "C:\Users\User\Documents\python\project1.py", line 70, in main
    print(pmtOptions(APR,periods_year, term,loan_amount))
  File "C:\Users\User\Documents\python\project1.py", line 46, in pmtOptions
    EMI = (loan_amount*pow((intRate/12)+1, (terms[x]))*intRate/12)/(pow(intRate/12+1, (terms[x])) - 1)
TypeError: 'int' object is not subscriptable

我的完整代码如下。主要功能似乎运行正常,直到我必须调用第三个函数。我能够调整一些字符串格式,但我似乎无法弄清楚让值正常工作,所以他们打印。我也尝试过更改主函数,但是必须再次修改pmtOptions函数中的变量。我能够通过加入我的一个列表来修复类似的问题来打印它,但是我不明白我应该用一个等式来做这个,或者如果还有别的我做错了。我是编码的初学者,所以我不知道是什么问题..

#payment options
def pmtOptions(APR,periods_year, term,loan_amount):

#first save rows and columbs as a list to access them again [interest rates]
    interestRates =[]
    apr = APR-1.5
#while loop for interest rates APR can be any APR have to iterate over halves 
    while apr <= (APR+1.5):
        interestRates.append(apr)
        apr = apr  + .5
#list for term values
        periods_year = int(periods_year)
        term = int(term)
        term_list =[]
        terms = (periods_year*term)
#while loop have to iterate for 6 in rows
    while terms <= (periods_year*term):
        term_list.append(terms)
        terms = terms + 6
#headers for table
    print("\n %35s %20s "%(" ", "Alternative Loan Payment Table"))
    print("%35s %20s "%(" ", "=============================="))
    print("%42s %20s "%(" ", "Interest Rates"))
    print("\n %10s "%(" "), end="")
#interest rate headigs with for loop

    for apr in interestRates:
        print(" %8.2f%% "%(apr), end = "")
#other heading
    print("\n %8s "%("# Payments"), end = "") #note to self to start new line had to take print out of for loop
#more headings
    for apr in interestRates:
         print("%8s "%("========"), end = "")
#term values remember new line
    for x in range(0,len(term_list)):
        print("\n %8s ".join(str(x) for x in term_list))

#intrest rate value
    for y in range(0,len(interestRates)):
        intRate = interestRates[x]/100.0
#filling in EMI equation for values
    EMI = (loan_amount*pow((intRate/12)+1, (terms[x]))*intRate/12)/(pow(intRate/12+1, (terms[x])) - 1)
#printing out values lets hope this works
    print(" $%7.2f "%(EMI),end = "") #remember do not include % in parens

#creating the main function part 1
def main():
    print("This program is used to analyze commercial real estate loans.")

#function variables
    purchase_price = input("Please input original purchase price: ")
    down_payment = input("Please input total down payment: ")
    APR = float(input("Please input APR: "))
    periods_year = input("Please input the number of payments per year: ")
    #periods_year = int(periods_year)
    term = input("Please input loan length in years: ")
    #term = int(term)
    first_payment = input("Please input date of first payment in form MM/DD/YYYY: ")
    loan_amount = float(purchase_price) - float(down_payment)
#seperating str
    dates = first_payment.split('/')
    day = int(dates[0])
    month = int(dates[1])
    year = int(dates[2])
#calling payment function
    print(pmtOptions(APR,periods_year, term,loan_amount))
main()


#loancalc function
def loanCalc(APR, period_year, loan_amount, total_payments):
#interest charged per period
    r = APR/(period_year)
#amount paid in each period
    P = (r * loan_amount)/(1- (1+r) ** (-(total_payments)))
#amount paid over life of loan
    final_value = (P * total_payments)
#part 1 calculations with main variables
    total_payments = float(term) * float(down_payment)

#total_amount = loanCalc(APR, periods_year, loan_amount, total_payments)
    end_date = str(day) + "/" + str(month) + "/" + str(year + int(term))
    total_interest = r*total_payments
#remember to return not print
    return final_value

0 个答案:

没有答案