0

I am aware that Python doesn't have a switch case like C++, but I'm trying to figure out how to go about it and found something, but not sure if it's implemented correctly.

So I have something like:

def choiceone():
    choiceone statement here

def choicetwo():
    choicetwo statement here

def switching(x):
    switcher= {1: choiceone(),
               2: choicetwo(),
              }
    func = switcher.get(x, 0)
    return func()


def main():
    user_input=input("Choice: ")
    switching(user_input)

main()

It's great that it prompts user for input, but regardless of number I write, it always runs choiceone.

I'm trying to see how I can go in invoking a function based on user selection.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Reginald
  • 23
  • 5

3 Answers3

1

Convert your user_input from string to int and don't call the functions in your switcher dictionary:

def choiceone():
    # choiceone statement here
    print "function 1"

def choicetwo():
    # choicetwo statement here
    print "function 2"

def switching(x):
    switcher= {1: choiceone, # Don't call the function here
               2: choicetwo,
              }

    func = switcher.get(x, 0)
    func() # call the choice of function here


def main():
    user_input = int(input("Choice: ")) # Convert to int
    switching(user_input)

main()
Abhishek Balaji R
  • 665
  • 10
  • 25
1

By calling the switching function, you are storing the result of the choiceone function as the value of the 1 key because you called the function.

You just need the function name without the parenthesis to hold the function reference.

Same with the return statement, unless you want to call the function there. If you want to return the function itself, you could call switching(user_input)() because switching(user_input) would return a function handle

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Here's a working example of your code:

def choiceone():
    print("choiceone")


def choicetwo():
    print("choicetwo")


def default():
    print("default")


def switching(x):
    return {
        1: choiceone,
        2: choicetwo,
    }.get(x, default)


if __name__ == "__main__":
    user_input = int(input("Choice: "))

    your_choice = switching(user_input)
    your_choice()

As you can see, in the above code I'm returning just the functions and storing them into your_choice variable, then i can just run them as any other function your_choice()

BPL
  • 9,632
  • 9
  • 59
  • 117
  • I get "TypeError: 'int' object is not callable. – Reginald Aug 14 '16 at 23:18
  • @Reginald Probably it was because you're using python 3.x, I've edited the code to cast your input to integer, please try again – BPL Aug 14 '16 at 23:28
  • ahhhhhhh thanks, it works now! didn't know they changed in the versions – Reginald Aug 14 '16 at 23:34
  • @Reginald Glad to hear that :) , btw, the best way to thanks over this site is just accepting the answer you consider it helped you the most – BPL Aug 14 '16 at 23:43