-1

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest rate of 1 %.

I have inserted the code I used in VBA for this specific function so you have an Idea of what I want / How it works although I have deleted various lines as I have only added the code to give a better visual of what I am trying to do.

creditRate = InputBox("Please enter credit rating:")

If creditRate = "AAA" Then GoTo intcalc Else
If creditRate = "A" Then GoTo intcalc Else
If creditRate = "BBB" Then GoTo intcalc Else
If creditRate = "BB" Then GoTo intcalc Else
If creditRate = "CCC" Then GoTo intcalc Else
If creditRate = "DDD" Then GoTo intcalc Else

If creditRate = "AAA" Then intRate = 0.01 Else
If creditRate = "A" Then intRate = 0.03 Else
If creditRate = "BBB" Then intRate = 0.05 Else
If creditRate = "BB" Then intRate = 0.06 Else
If creditRate = "CCC" Then intRate = 0.08 Else
If creditRate = "DDD" Then intRate = 0.1 Else
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
JPWilson
  • 691
  • 4
  • 14
  • You **DEFINITELY** don't have that code in VBA - that If-Else syntax isn't legal. Also, find a way to avoid `GoTo` jumps. 99.99999% of the time they're avoidable. – Mathieu Guindon Sep 27 '17 at 17:00

2 Answers2

0

In Python this would most likely be computed using a dict, a hash-based data structure that allows lookup of (fairly) arbitrary keys. Such a dict can be created as follows

rate_dict = {"AAA": 0.01, "A": 0.03, "BBB", 0.05, "BB", 0.06, "CCC": 0.08, "DDD": 0.1}

You would then set the interest rate (using Python's standard naming conventions) using

int_rate = rate_dict[credit_rate]

If credit_rate is set from user input you may need to check whether it's valid or not. You can do that with

if credit_rate in rate_dict:
    ...

If you want to ask the user for a valid input, start with an invalid value and iterate until the user has provided a valid one. A simple way to do this would be

credit_rate = '*'
while credit_rate not in rate_dict:
    credit_rate = input("Credit rating: ")

If you wanted to provide error messages then an infinite loop with a break on an acceptable value might be more readable.

while True:
    credit_rate = input("Credit rating: ")
    if credit_rate in rate_table:
        int_rate = rate_dict[credit_rate]
        break
    print(credit_rate, "is not a known credit rating"

Readers using Python 2 should take care to use the raw_input built-in, since in that older version input tries to evaluate the input as a Python expression.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • For the validity check, how can I change it so that instead of it cancelling the script it loops until the user has put in a valid input. For example for loan amount i did this. I cant seem to format the comment reply so the code is messed up sorry but im sure you get what im going after. .loanAmt = 0 while True: try: LoanAmt = int(input ('What Is Your Desired Loan Amount? ')) except ValueError: print("Not A Valid Choice!") continue else: print('Your Desired Loan Amount is £ ' + str(LoanAmt)) break – JPWilson Sep 27 '17 at 22:16
0

Python also provides a default dictionary that returns a default value if key does not exist in the dictionary.

from collections import defaultdict
rates = defaultdict(lambda: None, { 
                     "AAA" : 0.01, "A" : 0.03,
                     "BBB" : 0.05, "BB" : 0.06, 
                     "CCC" : 0.08, "DDD" : 0.1  
                      })

So

rate = rates['AAA'] #rate = 0.01

and

rate = rates['D']) #rate = None