1

I am creating a financial modeling tool that a user can use. I want the user to be able to be able to enter what method they want to use by typing the variable name. However, with the code I have, the raw_input just gives out a string of what they entered, not the variable name itself

loose = 5
accurate = 10
extreme = 15

method = raw_input('Would you like the calculation to be loose, accurate, or extreme in its precision?: ") #the user would either type loose, accurate, or extreme

calculation = x*y*method #x and y is just a generic calculation and method should be equal to either 5, 10, or 15 based on whether the user chose loose, accurate, or extreme

I get an error that says method is not an int. What can I do to have it equal to the loose, accurate, or extreme values above?

0

3 Answers 3

5

Change your variables into a dictionary and then use it to lookup the corresponding integer based on the user's input:

values = {'loose': 5,
          'accurate': 10,
          'extreme': 15}

method = raw_input('Would you like the calculation to be loose, accurate, or extreme in its precision?: ') #the user would either type loose, accurate, or extreme

calculation = x*y*values[method] #x and y is just a generic calculation and method should be equal to either 5, 10, or 15

You can also use values.get(method, 0) in place of values[method], if the users enters something that is not in your dictionary then 0 will be used.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a good way to do it. You will just need to make sure to handle the situation where someone puts in a method that doesn't match one of your choices.
@Isowen Good point; I edited to show use of values.get(method, 0) which would handle the case of an input not being in the dictionary.
1

The bestOne way would be something like:

loose = 5
accurate = 10
extreme = 15

method_selection = raw_input('Would you like the calculation to be loose, accurate, or extreme in its precision?: ')

if method_selection == "loose":
    method = loose
elif method_selection == "accurate":
    method = accurate
else:
    method = extreme

calculation = x*y*method

Adjust the order of the if statements to give you the proper "default" (ie someone puts in a method that doesn't match one of yours).

2 Comments

The best way? I initially thought of this as well, but using a dictionary as per Jkdc's answer is much better. You can simply add keys to the dictionary instead of adding if/else code. Not to mention the simplicity and ability to add/remove keys at run-time.
I agree, probably not the best way. Updating to reflect this.
0

A direct solution is to use eval.

method = eval(raw_input(...))

Note, that it's not recommended to use eval, since it sets a user free to execute whatever he/she wants.

2 Comments

While a potential method, it is usually best to avoid eval if at all possible.
@ Yeah, I've added that to the answer along with an explanation. Yet, it is an option one should know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.