I created a Python class called Score
. I intend to use it to assign scores depending on two parameters.
- the type of score,
score_name
, and - the
user_input
.
If I want to get the score of a loan term I will enter 'loan_term'
as the score_name
and 36
as user_input
. The method Score.get_score
then calculates the score and returns a value.
The idea is to be able to re-use this class as and when needed and to be able to modify the code outside of the main application code.
I would like a review of the implementation of my class. Have I deviated from standards or is there a better way to implement this?
class Score:
def __init__(self, score_name):
self.score_name = score_name
def get_score_name(self):
return self.score_name
def get_score(get_score_name, user_input):
# loan term score grading
if get_score_name == 'loan_term' and user_input <= 18:
i = 5
elif get_score_name == 'loan_term' and user_input <= 24:
i = 4
elif get_score_name == 'loan_term' and user_input <= 48:
i = 3
# loan reason score grading
elif get_score_name == 'loan_reason' and user_input == 0:
i = 3.5
elif get_score_name == 'loan_reason' and user_input == 1:
i = 2.0
elif get_score_name == 'loan_reason' and user_input == 2:
i = 1.5
elif get_score_name == 'loan_reason' and user_input == 3:
i = 2.5
elif get_score_name == 'loan_reason' and user_input == 4:
i = 1.5
elif get_score_name == 'loan_reason' and user_input == 5:
i = 2.0
elif get_score_name == 'loan_reason' and user_input == 6:
i = 0.5
# job security score grading
elif get_score_name == 'job_security' and user_input == 0:
i = 1.0
elif get_score_name == 'job_security' and user_input == 1:
i = 2.0
elif get_score_name == 'job_security' and user_input == 2:
i = 3.0
elif get_score_name == 'job_security' and user_input == 3:
i = 4.0
elif get_score_name == 'job_security' and user_input == 4:
i = 5.0
# average apr score grading
elif get_score_name == 'average_apr' and user_input <= -1.0:
i = 5
elif get_score_name == 'average_apr' and user_input <= -0.6:
i = 4
elif get_score_name == 'average_apr' and user_input < 0.5:
i = 3
elif get_score_name == 'average_apr' and user_input <= 1.0:
i = 2
elif get_score_name == 'average_apr' and user_input > 1.0:
i = 1
# monthly expenses score grading
elif get_score_name == 'monthly_expenses' and user_input < 0.10:
i = 5
elif get_score_name == 'monthly_expenses' and user_input < 0.20:
i = 4
elif get_score_name == 'monthly_expenses' and user_input < 0.30:
i = 3
elif get_score_name == 'monthly_expenses' and user_input < 0.40:
i = 1
elif get_score_name == 'monthly_expenses' and user_input > 0.40:
i = 0
# monthly surplus score grading
elif get_score_name == 'monthly_surplus' and user_input < 0.20:
i = 5
elif get_score_name == 'monthly_surplus' and user_input < 0.35:
i = 4
elif get_score_name == 'monthly_surplus' and user_input < 0.45:
i = 3
elif get_score_name == 'monthly_surplus' and user_input <= 0.60:
i = 2
elif get_score_name == 'monthly_surplus' and user_input > 0.60:
i = 1
else:
i = 0
return i
# loan term test
loan_term_score = Score.get_score(Score('loan_term').get_score_name(), 21)
print(f'loan term score: {loan_term_score}')
# loan reason test
loan_reason_score = Score.get_score(Score('loan_reason').get_score_name(), 2)
print(f'loan reason score: {loan_reason_score}')
# job security test
job_security_score = Score.get_score(Score('job_security').get_score_name(), 3)
print(f'job security score: {job_security_score}')
# average apr test
average_apr_score = Score.get_score(Score('average_apr').get_score_name(), 1.0)
print(f'average apr score: {average_apr_score}')
# monthly expenses test
monthly_expenses_score = Score.get_score(Score('monthly_expenses').get_score_name(), 0.15)
print(f'monthly expenses score: {monthly_expenses_score}')
# monthly surplus test
monthly_surplus_score = Score.get_score(Score('monthly_surplus').get_score_name(), 0.55)
print(f'monthly surplus score: {monthly_surplus_score}')
average_apr
section, did you really mean< 0.5
or did you mean<= 0.5
the way all the rest of the cases are? \$\endgroup\$