贷款计算器使用类(OOP)不在python中运行

时间:2016-11-30 02:26:14

标签: python class oop

# Create a class called "Loan":
# Data fields in the Loan class include: Annual Interest Rate(Float),\
# Number of years of loan(Float), Loan Amount(Float), and Borrower's Name(string)
class Loan:
    # Create the initializer or constructor for the class with the above data fields.
    # Make the data fields private. 
    def __init__(self, annualInterestRate, numberOfYears, loanAmount, borrowerName):
        self.__annualInterestRate=annualInterestRate
        self.__numberOfYears=numberOfYears
        self.__loanAmount=loanAmount
        self.__borrowerName
    # Create accessors (getter) for all the data fields: 
    def getannualInterestRate(self):
        return self.__annualInterestRate
    def getnumberOfYears(self):
        return self.__numberOfYears
    def getloanAmount(self):
        return self.__loanAmount
    def getborrowerName(self):
        return self.__borrowerName
    # Create mutators (setters) for all the data fields:
    def setannualInterestRate(self):
        self.__annualInterestRate=annualInterestRate
    def setnumberOfYears(self):
        self.__numberOfYears=numberOfYears
    def setloanAmount(self):
        self.__loanAmount=loanAmount
    def setborrowerName(self):
        self.borrowerName=borrowerName
    # Create a class method: getMonthlyPayment - 
    def getMonthlyPayment(self,loanAmount, monthlyInterestRate, numberOfYears):
        monthlyPayment = loanAmount * monthlyInterestRate / (1
        - 1 / (1 + monthlyInterestRate) ** (numberOfYears * 12))
        return monthlyPayment;
    # Create a class method: getTotalPayment - 
    def getTotalPayment(self):
        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(self.monthlyPaymentVar.get()) * 12 \
        * int(self.numberOfYearsVar.get())
        self.totalPaymentVar.set(format(totalPayment, '10.2f'))

def main():
    loan1=Loan()
    print(input(float("Enter yearly interest rate, for exmaple, 7.25: ", loan1.annualInterestRate())))
    print(input(float("Enter number of years as an integer: ", loan1.getnumberOfYears())))
    print(input(float("Enter loan amount, for example, 120000.95: ", loan1.getloanAmount())))
    print(input(float("Enter a borrower's name: ", loan1.getborrowerName())))

    print("The loan is for", loan1.getborrowerName())
    print("The monthly payment is", loan1.getMonthlyPayment())
    print("The total payment is", loan1.getTotalPayment())

    print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit"))

    print(input(float("Enter a new loan amount: ")))
    print("The loan is for", loan1.getborrowerName())
    print("The monthly payment is", loan1.getMonthlyPayment())
    print("The total payment is", loan1.getTotalPayment())

main()

由于某些原因,我的程序没有运行。 我试图让用户更改贷款金额并重新打印新的贷款信息。我不知道我做错了什么,而且课程/ OOP是对我而言是新的,所以我在过去的一年里一直在做程序化,所以我非常挣扎。我知道这充满了众多错误 ......但我无处可去。所有在线课程的教程都非常模糊和理论化,并没有像我面对的那样提出具体的例子/场景。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

解释器生成的错误消息绰绰有余。

Loan()

您需要将用户输入的任何内容作为参数传递给类构造函数self.__borrowerName=borrowerName 。其他方法仅用于返回类变量,但所有初始化都在构造函数中完成。

此外,您的构造函数定义错误,请更正此行:

#include <stdio.h>


int main(){
int i=0;
 //setting the individual slot number for the array-- later used in the while loop
char guy[100];
printf("Enter the plain text:");
fgets(guy,100,stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right?

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string


    if ((guy[i]) >= 'A' && (guy[i]<= 'Z')){ //moves capital letter values up 1
        if (guy[i]=='Z'){
            guy[i]=guy[i]-25;
        }
        else {
        guy[i]=guy[i]+1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98
        }
        }
    if ((guy[i]) >= 'a' && (guy[i]) <= 'z'){// moves lower case letter values up 1
        if (guy[i]=='z'){
            guy[i]=guy[i]-25;
        }
        else{
        guy[i]=guy[i]+1;
        }
    }
    i++; //moves the array's interval up to the next "slot"

}
printf("Encrypted text is: %s\n",guy);

//Vigenere Cipher-- keyword is "apple"
//a = 1  value shift
//p = 16 value shift
//p = 16 value shift
//l = 17 value shift
//e = 5  value shift

printf("Enter the plain text: ");
fgets(guy,100,stdin);//takes user's input

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string

    if (i%5==0 || i==0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1
    guy[i] = guy[i]+1;
    }

    if ((i-1)%5==0 || i==1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16
        guy[i]=guy[i]+16;
    }
    if ((i-2)%5==0 || i==2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22
        guy[i]=guy[i]+16;
    }
    if((i-3)%5==0 || i==3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18
        guy[i]=guy[i]+17;
    }
    if((i-4)%5==0 || i==4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19
        guy[i]=guy[i]+5;
    }
    else {
    i++;
    }
    }
printf("Encrypted text is: %s\n",guy);
}