[英]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 = 0
和years_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.