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 ElseIf 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
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]breakprint(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.