I am having a problem with a loan calculator that I am building. It keeps telling me that the interest_rate variable it referenced before assignment. I'm fairly new to python and was wondering if anyone had any ideas as to what is wrong with my code. Any help would be greatly appreciated.

MIN_SALARY = 20000.0
MIN_YEARS = 2
MIN_CREDIT_SCORE = 400

#We will get the customer's name, salary, length of time at job, and credit
#score.
def main():

    name = input("Enter customer's full name: (first last)")

    job_length = float(input("Enter how many years customer has been at their" \
                             " job: "))

    salary = float(input("Enter customer's yearly income (no commas, no $): "))

    credit_score = float(input("Enter customer's credit score: "))

    interest_rate = determine_loan_rate(salary, credit_score)

    percent = interest_rate * 100

    if job_length < MIN_YEARS:
        print("Unfortunately, " ,name, " does not qualify for a loan" \
                      " at this time.")
    elif salary < MIN_SALARY:
        print("Unfortunately, " ,name, " does not qualify for a loan" \
                      " at this time.")
    elif credit_score < MIN_CREDIT_SCORE:
                print("Unfortunately, " ,name, " does not qualify for a loan" \
                      " at this time.")
    results()


def determine_loan_rate(salary, credit_score):
    if salary >= MIN_SALARY and salary <= 40000:
        if credit_score >= MIN_CREDIT_SCORE and credit_score <= 600:
            interest_rate = .10
        elif credit_score > 600:
            interest_rate = .06
    elif salary > 40000:
        if credit_score >=MIN_CREDIT_SCORE and credit_score <=600:
            interest_rate = .08
        elif credit_score > 600:
            interest_rate = .05
    return interest_rate
def results():
    print("Based on the supplied input, " ,name, " is approved for a loan at "\
          " a %", format(percent, '.2f'))


main()

Recommended Answers

All 3 Replies

This error occurs when one tries to use a variable before it is defined at the left side of an =. The problem in your function determine_loan_rate() is that the ifs and elifs don't cover all the possible cases. In certain circumstances, the program can reach return interest_rate with none of the 4 cases satisfied.

Hopefully this is not a real loan approval software.

local variables of results function are not also defined, maybe you missed parameters?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.