Cannot Pass Returned Values From Function To Another Function In Python
Solution 1:
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.
classLoan_Checker():
def__init__(self):
self.salary = 0
self.years_on_job = 0
self.request_salary()
self.request_years()
self.check_if_qualified()
defrequest_salary(self):
x = raw_input('Enter your annual salary: ')
try:
self.salary = int(x)
except:
print("Please enter a valid number")
self.request_salary()
defrequest_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()
defcheck_if_qualified(self):
if self.salary >= 30000and self.years_on_job >= 2:
print'You qualify for the loan. 'else:
print'You do not qualify for this loan. '
Loan_Checker()
Solution 2:
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
classloan_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()
defsalary_check(self):
input_counter = 0# local variable# Get the customer's annual salary.
salary = Nonewhile salary isNone:
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 salaryreturn salary
defwork_exp_check(self):
input_counter = 0#local variable to this function# Get the number of years on the current job.
years_on_job = Nonewhile years_on_job isNone:
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_jobreturn years_on_job
defassess_customer(self):
# Determine whether the customer qualifies.ifint(self.salary) >= 30000.0andint(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 (i.e. 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.
Solution 3:
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)
Post a Comment for "Cannot Pass Returned Values From Function To Another Function In Python"