Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello! I am getting an error in Python in one spot, I\'m going to upload my whol

ID: 3906754 • Letter: H

Question

Hello! I am getting an error in Python in one spot, I'm going to upload my whole code but i'll space out what the problem text is

Here is the error I'm getting

from tkinter import *
import tkinter.messagebox
from tkinter import ttk
import tkinter.simpledialog
import tkinter as tk

#Classes and make window
class LoanCalculator:
def __init__(self):
window = Tk()
window.title("Loan Calculator")

  
#Gui stuff
Label(window, text = "Annual Interest Rate").grid(row = 1, column = 1, sticky = W)
Label(window, text = "Number of Years").grid(row = 2, column = 1, sticky = W)
Label(window, text = "Loan value").grid(row = 3, column = 1, sticky = W)
Label(window, text = "Monthly Payment value").grid(row = 4, column = 1, sticky = W)
Label(window, text = "Total Payment value").grid(row = 5, column = 1, sticky = W)

#second part
self.annualInterestRateVar = StringVar()
Entry(window, textvariable = self.annualInterestRateVar, justify = RIGHT).grid(row = 1, column = 2)
self.numberOfYearsVar = StringVar()
Entry(window, textvariable = self.numberOfYearsVar, justify = RIGHT).grid(row = 2, column = 2)
self.loanAmountVar = StringVar()
Entry(window, textvariable = self.loanAmountVar, justify = RIGHT).grid(row = 3, column = 2)
self.monthlyPaymentVar = StringVar()
lblMonthlyPayment = Label(window, textvariable = self.monthlyPaymentVar).grid(row = 4, column = 2, sticky = E)
self.totalPaymentVar = StringVar()
lblTotalPayment = Label(window, textvariable = self.totalPaymentVar).grid(row = 5, column = 2, sticky = E)
  


#Payment and save buttons!
btComputePayment = Button(window, text = "Compute Payment", command = self.computePayment).grid(row = 6, column = 1, sticky = E)
btSaveLoantoFile = Button(window, text = "Save Loan to File", command = self.saveFile).grid(row = 6, column = 2, sticky = E)

  


#DOUBLE CHECK INDENTS!!!!!!!!!!!!
self.twidget = Text(window, width=80, height=24, wrap='none')
self.twidget.grid(column = 1, columnspan = 2)
#loop it real good
window.mainloop()
  
  
#Part 3
def computePayment(self):
try:

self.twidget.insert(1.0, "")
monthlyPayment = self.getMonthlyPayment(
float(self.loanAmountVar.get()),
float(self.annualInterestRateVar.get()) / 1200,
int(self.numberOfYearsVar.get()))
self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))
totalPayment = float(monthlyPayment) * 12
* int(self.numberOfYearsVar.get())
self.totalPaymentVar.set(format(totalPayment, '10.2f'))

#amortization from accounting to each object
loanAmount = int(self.loanAmountVar.get())
annualInterestRate = float(self.annualInterestRateVar.get())
numberOfYears = int(self.numberOfYearsVar.get())
monthlyPayment = float(self.monthlyPaymentVar.get())
totalPayment = float(self.totalPaymentVar.get())
monthlyInterestRate = annualInterestRate / 12
  
#COUBLE CHECK SPELLING BELOW
textForWidget = ""
s = ""
begBal = loanAmount
s = " Pmt# " " Interest" " Prin Pmt" " Remaining Prin" + " "
textForWidget = textForWidget + s
for i in range(1, numberOfYears * 12 + 1):
interestAmount = round(begBal * (annualInterestRate / (12 * 100.0)), 2)
principalAmount = round(monthlyPayment - interestAmount, 2)
endingBalance = round(begBal - principalAmount, 2)
begBal = endingBalance
s = "{0:d} {1:>8} {2:>8} {3:>10} ".format(i, "$"+str(format(interestAmount, ",.2f")),
"$"+str(format(principalAmount, ",.2f")), "$"+str(format(endingBalance,",.2f")))
textForWidget = textForWidget + s

self.twidget.insert(1.0, textForWidget)
  
#if then error statement!
except ValueError:
tkinter.messagebox.showerror("Calculation Error", "Please make sure to enter numeric values for interest rate, years, and loan amount")

#Quad check with textbook!  
def getMonthlyPayment(self, loanAmount, monthlyInterestRate, numberOfYears):
monthlyPayment = loanAmount * monthlyInterestRate / (1- 1 / (1 + monthlyInterestRate) ** (numberOfYears * 12))
return monthlyPayment;
  

def saveFile(self):
loanAmount = int(self.loanAmountVar.get())
annualInterestRate = float(self.annualInterestRateVar.get())
numberOfYears = int(self.numberOfYearsVar.get())
monthlyPayment = float(self.monthlyPaymentVar.get())
totalPayment = float(self.totalPaymentVar.get())
monthlyInterestRate = annualInterestRate / 12
name = tkinter.simpledialog.askstring("Loan Recipient", "Enter the name of the loan recipient")
#New file New name!!
filename = name + "loan document.txt"
outfile = open(filename, "w")
#top name of file!
header = " Loan Document for " + name
outfile.write(header)
#spacing for file
line1 = " Loan Amount: ${0:,.2f} Interest Rate: {1:.2f}% Nbr Years: {2:d} " .format(loanAmount, annualInterestRate,numberOfYears)
outfile.write(line1)
line2 = "Monthly Payment: ${0:,.2f} Total Payment: ${1:,.2f} ".format(monthlyPayment, totalPayment)
outfile.write(line2)
line3 = " Amortization Schedule:" + " "
outfile.write(line3)
line4 = " Pmt# " " Interest" " Prin Pmt" " Remaining Prin" + " "
outfile.write(line4)
begBal = loanAmount

#Part DONEEEE
for i in range(1, numberOfYears * 12 + 1):
interestAmount = round(begBal * (annualInterestRate / (12 * 100.0)), 2)
principalAmount = round(monthlyPayment - interestAmount, 2)
endingBalance = round(begBal - principalAmount, 2)
begBal = endingBalance
s = "{0:d} {1:>15} {2:>15} {3:>10} ".format(i, "$"+str(format(interestAmount, ",.2f")),
"$"+str(format(principalAmount, ",.2f")), "$"+str(format(endingBalance,",.2f")))
outfile.write(s)
#Closing
outfile.close()
LoanCalculator()

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Inte 1) on win32 Type "copyright", "credits" or "license" for more information. RESTART : C:/Users/Alex? s/ Desktop/ FisherPA6 .py = Traceback (most recent call last) File "C:/Users/Alexis/Desktop/FisherPA6.py, line 136, in LoanCalculator ) File "C:/Users/Alexis/Desktop/FisherPA6.py",line 41, in init btComputePayment = Button (window, text = "Compute Payment", command = self.c omputePayment).grid (row= 6, column= 1, sticky= E) AttributeError LoanCalculator' object has no attribute 'compute Payment'

Explanation / Answer

(Provided proper indendation/spacing is followed,the issue here is trying to call/use computePayment method before its definition.

For avoiding such error computePayment method must be defined before using it.

You can bring its definition before the Button statement.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote