0

I am writing code where the user inputs a string that is one of possible choices (parameters which where previously inputted).

On the basis of the choice made, the code run the correct function which is used to ask the user to input the data and to verify it.

This is my code:

correct = input("Are all the data inserted correct? Yes/No \n")
while correct.lower() == 'no':
    change = input("What would you like to change? S0, k, r, u, d, volatility, T, nodes, style, type \n")
    if change.lower() == 's0':
        S0 = in_S0()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 'k':
        k = in_k()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 'r':
        r = in_r()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() in ['u','d','vol','volatility']:
        u, d, vol = in_asset()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 't':
        T = in_T()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 'nodes':
        nodes = in_nodes()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 'style':
        style = in_style()
        correct = input("Are all the data inserted correct? Yes/No \n")
    if change.lower() == 'type':
        types = in_types()
        correct = input("Are all the data inserted correct? Yes/No \n")

All the functions (in_...) are defined in another file and imported at the beginning.

While I am pleased with its clearance and its functioning, I suppose this is not the best way to do it. Is there a better option?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Use a dictionary and map the string to the function. – David Zemens Aug 27 '19 at 15:02
  • 2
    This seems to be a duplicate of [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python). – Booboo Aug 27 '19 at 15:03
  • Go with different DS like `dict`. On the side note try `if..elif` in order to improve performance if those cases are mutually exclusive – mad_ Aug 27 '19 at 15:03
  • Have you tried using a dictionary object with keys being your `change` parameter and values being the input message and possibly the `in_` function? – stahamtan Aug 27 '19 at 15:03
  • You have to determine what is common in all the methods. Rewriting that input question many times can be reduced to using a constant, for example. – ergonaut Aug 27 '19 at 15:06
  • Do you use the variables like `S0`, `nodes` etc elsewhere? Or simply to capture the return value of the specific functions? – David Zemens Aug 27 '19 at 15:07
  • Yes, I use them in the code that follows. Thank you for the useful answers. I followed one comment that i can't see anymore. I have used the elif and put correct = input(...) at the end of the while cycle. – Morrone Marco Aug 27 '19 at 19:32

1 Answers1

0

Yes, there is a better way. Unfortunately, python does not have a switch statement, but it does have dictionaries. What you can do, is something like this:

mapping = {"s0":in_S0, "k":in_k .... }
mapping[change.lower()]()

Do note however, that there is nothing inherently wrong with having a few if-statements in a row, sometimes that's just the best solution. If you are working with inexperienced Developers, writing code like stated above may just confuse them.

3ch0
  • 173
  • 1
  • 7