简体   繁体   中英

Cannot pass returned values from function to another function in python

My goal is to have a small program which checks if a customer is approved for a bank loan. It requires the customer to earn > 30k per year and to have atleast 2 years of experience on his/her current job. The values are get via user input. I implemented regexs to validate the input to be only digits without any strigns or negatives, nor 0.

But the 3rd function asses_customer is always executing the else part. I think everytime the parameters are either None, either 0

here's the source code:

import sys
import re
import logging
import self as self


class loan_qualifier():

    # This program determines whether a bank customer
    # qualifies for a loan.

    def __init__(self): #creates object
        pass

def main():

        salary_check()
        work_exp_check()
        asses_customer(salary = 0, years_on_job = 0)


def salary_check():

        input_counter = 0  # local variable

        # Get the customer's annual salary.
        salary = raw_input('Enter your annual salary: ')
        salary = re.match(r"(?<![-.])\b[1-9][0-9]*\b", salary)

        while not salary:

            salary = raw_input('Wrong value. Enter again: ')
            salary = re.match(r"(?<![-.])\b[1-9][0-9]*\b", salary)

            input_counter += 1

            if input_counter >= 6:
                print ("No more tries! No loan!")
                sys.exit(0)
            else:
                return salary


def work_exp_check():

        input_counter = 0 #local variable to this function

        # Get the number of years on the current job.
        years_on_job = raw_input('Enter the number of ' +
                                 'years on your current job: ')
        years_on_job = re.match(r"(?<![-.])\b[1-9][0-9]*\b", years_on_job)

        while not years_on_job:

            years_on_job = raw_input('Wrong work experience. Enter again: ')
            years_on_job = re.match(r"(?<![-.])\b[1-9][0-9]*\b", years_on_job)

            input_counter += 1

            if input_counter >= 6:
                print ("No more tries! No loan!")
                sys.exit(0)
            else:
                return years_on_job

def asses_customer(salary, years_on_job):

        # Determine whether the customer qualifies.
        if salary >= 30000.0 or years_on_job >= 2:

            print 'You qualify for the loan. '
        else:
            print 'You do not qualify for this loan. '

# Call main()
main()

You have stated:

It requires the customer to earn > 30k per year and to have at least 2 years of experience on his/her current job.

We can write some simple statements that request a number and if a number is not given then ask for that number again.

The following code is a very simple approach to achieving that goal.

class Loan_Checker():
    def __init__(self):
        self.salary = 0
        self.years_on_job = 0

        self.request_salary()
        self.request_years()
        self.check_if_qualified()

    def request_salary(self):
        x = raw_input('Enter your annual salary: ')
        try:
            self.salary = int(x)
        except:
            print("Please enter a valid number")
            self.request_salary()

    def request_years(self):
        x = raw_input('Enter the number of years on your current job: ')
        try:
            self.years_on_job = int(x)
        except:
            print("Please enter a valid number")
            self.request_years()

    def check_if_qualified(self):
        if self.salary >= 30000 and self.years_on_job >= 2:
            print 'You qualify for the loan. '
        else:
            print 'You do not qualify for this loan. '

Loan_Checker()

You have a few errors in your code, and I've refactored it to use the class structure you seemed to want to imply.

import sys
import re
import logging

class loan_qualifier():

    # This program determines whether a bank customer
    # qualifies for a loan.

    def __init__(self): #creates object
        self.salary = self.salary_check()
        self.years_on_job = self.work_exp_check()


    def salary_check(self):

        input_counter = 0  # local variable

        # Get the customer's annual salary.
        salary = None

        while salary is None:
            if input_counter >= 6:
                print ("No more tries! No loan!")
                sys.exit(0)
            elif input_counter >= 1:
                print ("Invalid salary.")

            salary = raw_input('Enter your salary: ')
            salary = re.match(r"(?<![-.])\b[1-9][0-9]*\b", salary).group(0)
            input_counter += 1

        # broke out of loop, so valid salary
        return salary




    def work_exp_check(self):

        input_counter = 0 #local variable to this function

        # Get the number of years on the current job.
        years_on_job = None

        while years_on_job is None:
            if input_counter >= 6:
                print ("No more tries! No loan!")
                sys.exit(0)
            elif input_counter >= 1:
                print ("Invalid year amount")

            years_on_job = raw_input('Enter the number of years at your current job: ')
            years_on_job = re.match(r"(?<![-.])\b[1-9][0-9]*\b", years_on_job).group(0)

            input_counter += 1

        # broke out of loop, so valid years_on_job
        return years_on_job



    def assess_customer(self):

        # Determine whether the customer qualifies.
        if int(self.salary) >= 30000.0 and int(self.years_on_job) >= 2:
            print 'You qualify for the loan. '
        else:
            print 'You do not qualify for this loan. '

if __name__ == "__main__":
    lq = loan_qualifier()
    lq.assess_customer()

Some of the errors fixed include the way you were calling assess_customer initially (you were assigning 0's to both values in the function call), as well as the spelling of assess :p. Your condition in assess_customer should also have been an and instead of an or (you wanted both conditions to be true for them to qualify, not for either condition to be true).

You actually don't even really need to do the:

self.salary = self.salary_check()
self.years_on_job = self.work_exp_check()

lines. You could just directly assign the class variables in the functions (ie instead of returning, just set self.salary = blah in salary_check). That's kind of a personal choice thing though. I think this makes it clear.

Hopefully this is all clear to you. Let me know if you have any questions. The code can be called by simply typing python NAME_OF_YOUR_FILE.py.

Edit: I didn't realize how broken the salary and years checks were, the new code should fix them.

Edit: Fixed the regex results in this version. My bad.

In this fragment you pass third function always salary = 0 and years_on_job = 0

Try this way:

salary = salary_check()
years_on_job = work_exp_check()
asses_customer(salary, years_on_job)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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