簡體   English   中英

無法將返回的值從函數傳遞給python中的另一個函數

[英]Cannot pass returned values from function to another function in python

我的目標是建立一個小程序,檢查客戶是否獲得銀行貸款批准。 它要求客戶每年收入> 30k,並且至少要有2年的當前工作經驗。 這些值是通過用戶輸入獲得的。 我實現了正則表達式,以驗證輸入內容是否為純數字,沒有任何反斜線或負數,也不為0。

但是第三個函數asses_customer總是執行else部分。 我認為每次參數要么為None,要么為0

這是源代碼:

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()

您已聲明:

它要求客戶每年收入> 30k,並且在其當前工作中至少有2年的經驗。

我們可以編寫一些簡單的語句來請求一個數字,如果沒有給出數字,則再次要求該數字。

以下代碼是實現該目標的非常簡單的方法。

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()

您的代碼中有一些錯誤,我將其重構為使用您似乎想暗示的類結構。

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()

已修復的一些錯誤包括您最初調用評估_客戶的方式(您在函數調用中為兩個值都分配了0),以及評估:p的拼寫。 您在validate_customer中的條件也應該是和而不是或(您希望兩個條件都成立才使它們有資格,而不是兩個條件都成立)。

您實際上甚至根本不需要執行以下操作:

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

線。 您可以直接在函數中分配類變量(即,不返回,只需在salary_check中設置self.salary = blah)。 不過,這是個人選擇的事情。 我認為這很清楚。

希望對您來說一切都清楚。 如果您有任何疑問,請告訴我。 只需鍵入python NAME_OF_YOUR_FILE.py,即可調用該代碼。

編輯:我不知道薪水和年薪有多壞,新的代碼應該解決它們。

編輯:修復了此版本中的正則表達式結果。 我的錯。

在此片段中,您傳遞的第三個功能始終salary = 0 years_on_job = 0years_on_job = 0

嘗試這種方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM