Gathering numerical data from a string input

2024/10/10 14:22:17

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
Answer

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.

https://en.xdnf.cn/q/118443.html

Related Q&A

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …

Django cant find template dir?

Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the sa…

Python Programming Loop

Im doing an assignment where I have to conduct a quiz for different topics. This is my code so far.print("Hello and welcome to Shahaads quiz!") #Introduction name = input("What is your n…

How to fix stale element error without refreshing the page

Trying to get details of Tyres on this page. https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL . Each tyre has different FINITIONS. The price and other details are different for each FINITIONS. I w…

Fastest way in numpy to get distance of product of n pairs in array

I have N number of points, for example: A = [2, 3] B = [3, 4] C = [3, 3] . . .And theyre in an array like so: arr = np.array([[2, 3], [3, 4], [3, 3]])I need as output all pairwise distances in BFS (Bre…

How to get argument to ignore part of message

I just wondering how to get the if statement(if 0 < int(message.content)< 153:) to only test part of the message, not the full message.content. Eg: if I put in 1s 100, I want it to test if ONLY t…

how can I limit the access in Flask

I create a project to simulate login my companys website.And put it in my server to let others to use.But the company website has a limit with single ip can only open 2 sessions.So when more than 2 my …

Multiple images numpy array into blocks

I have a numpy array with 1000 RGB images with shape (1000, 90, 90, 3) and I need to work on each image, but sliced in 9 blocks. Ive found many solution for slicing a single image, but how can I obtai…