【发布时间】:2017-09-22 21:49:42
【问题描述】:
我的目标是创建一个小程序来检查客户是否获得银行贷款的批准。它要求客户每年收入 > 30k,并且在他/她目前的工作中至少有 2 年的经验。这些值是通过用户输入获得的。我实现了正则表达式来验证输入只是没有任何字符串或负数的数字,也不是 0。
但是第三个函数 asses_customer 总是在执行 else 部分。我认为每次参数要么是无,要么是 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()
【问题讨论】:
-
下次会记住的,谢谢
-
你只能用
asses_customer(salary = 0, years_on_job = 0)打电话给asses_customer;你期望参数是什么? (另外,动词“assess”的结尾有两个S;一个S给你一堆驴子。) -
你能帮我以正确的方式做吗?
-
我没有说它会改变任何东西。出于某些原因,您应该始终确保具有准确的缩进。与您在问题中看到的内容相比,您的代码的实际格式可能会令人困惑,并且还可能导致代码执行出现问题。
标签: python python-2.7