for loop in a switch-dictionary for interactive menu
<p>I have a problem in a SWITCH /CASE Disctionary I am trying to implement. I got an example from <a href="http://www.pydanny.com/why-doesnt-python-have-switch-case.html" rel="nofollow">here</a> </p>

<p>I simply create an interactive menu in which you can choose more than 1 option at the same time in the order you choose them.</p>

<pre><code>ip = ["10.10.10.1","10.10.10.2"]

def display_menu():
    os.system('clear')
    main_title = "***** MENU *****"
    print main_title.center(50,' ')
    print("\n\n\t1. Ping Check\n\t2. CPU Load.")

#Allows you to choose more than one option
def pick_the_order():
    display_menu()
    order = map(int,raw_input("Choose (separated by spaces if more than one): ").split())
    return order   
</code></pre>

<p>Once it chooses the options it calls the proper functions:</p>

<pre><code>def echo_pick(ips1):
  print "ECHO call", ips1

def snmp_pick(ips1):
  print "SNMP Call", ips1

def numbers_to_functions_to_strings(argument):
  global ip    
  for ips, option in zip(ip, argument):
    print "Ip:",ips
    print "option:", option
    switcher = {
        1: echo_pick(ips),
        2: snmp_pick(ips)
    }
    switcher.get(option, "That is not a valid option")

print numbers_to_functions_to_strings(pick_the_order())
</code></pre>

<p>The problem I am facing is that, when running the program it runs the for for each of the options, no matter if your option is 1 or 2. The output is something like this (when choosing option 1 and 2):</p>

<pre><code>Ip: 10.10.10.1
option: 1
ECHO call 10.10.10.1
SNMP Call 10.10.10.1
Ip: 10.10.10.2
option: 2
ECHO call 10.10.10.2
SNMP Call 10.10.10.2
None
</code></pre>

<p>And the output I am looking for is this (when choosing option 1 and 2):</p>

<pre><code>Ip: 10.10.10.1
option: 1
ECHO call 10.10.10.1
ECHO call 10.10.10.2
Ip: 10.10.10.2
option: 2
SNMP Call 10.10.10.1
SNMP Call 10.10.10.2
None
</code></pre>

<p>What is making the FOR going through all the options? Even when I choose only 1 option, say ECHO call, it should only call <code>echo_pick(ips1)</code>, but it still calls <code>snmp_pick(ips)</code></p>

<p>Can anybody can help me on what part of my logic is not working properly? It seems that is is bypassing the values in option and just runs them anyway.</p>

<p>You actually make the function call, and not use the reference to those functions.</p>

<p>If you define your dictionary as following:</p>

<pre><code>switcher = {
    1: func1(ips)
    2: func2(ips)
}
</code></pre>

<p>You already made the call to <code>func1</code> and <code>func2</code>. The solution here is just to make a reference to those functions, get the right one and execute it:</p>

<pre><code>def numbers_to_functions_to_strings(argument):
    # global ip: no need to make it global since you do not modify them.

    switcher = {
        1: echo_pick,  # &lt;-- reference to the function
        2: snmp_pick
    }

    for option in arguments:
        if option not in swticher:
            print("Invalid option:", option)
            continue

        func_to_call = switcher[option]  # &lt;-- pick the right function
        for ips in ip:  # NOTE: you should reverse ips and ip. Naming convention :)
            func_to_call(ips)  # &lt;-- call the function
</code></pre>

<p>Note that I didn't zip the arguments. Maybe it is because I didn't understand the problem but I think that you misuse the <code>zip</code> function here.</p>

python for-loop switch-statement