我的目标是制定一个小程序,检查客户是否获准获得银行贷款。它要求客户获得>每年30k,并且在他/她目前的工作中拥有至少2年的经验。这些值是通过用户输入获得的。我实现了正则表达式来验证输入只是没有任何符号或否定的数字,也不是0。
但是第3个函数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()
答案 0 :(得分:1)
您已声明:
它要求客户获得&gt;每年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()
答案 1 :(得分:1)
您的代码中有一些错误,我已经重构它以使用您似乎想要暗示的类结构。
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()
修复的一些错误包括您最初调用assess_customer的方式(您在函数调用中为这两个值指定了0&#),以及evaluate的拼写:p。您在assessment_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。
即可调用代码编辑:我没有意识到薪水和年份检查是如何破碎的,新代码应该修复它们。
编辑:修复了此版本中的正则表达式结果。我的坏。
答案 2 :(得分:0)
在此片段中,您始终传递第三个函数salary = 0
和years_on_job = 0
尝试这种方式:
salary = salary_check()
years_on_job = work_exp_check()
asses_customer(salary, years_on_job)