1

switch is not inbuilt in Python language so How can i achieve this concept in python for select one case

in other language code is :

`switch(value)  {   
case 0:
        print("i'm in Case 0");
        break; 
case 1:
        print("i'm in Case 1");
        break; 
case 2:
        print("i'm in Case 2");
        break; 
case 4:
        print("i'm in Case 4");
        break;
default:
        print("Wrong Value");}

How can i use this concept in Python pleae tell me any Technique for use it?

Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41

2 Answers2

1

As you said, there isn't built in functionality for switches in Python, however there are a couple of ways to achieve it.

The first approach (not a great solution), would be to simply use if statements:

if x == 0:
    print "X is 0\n"
elif x == 1:
    print "X is 1\n"
elif x == 2:
    print "X is 2r\n"
elif x == 3:
    print "X is 3\n"

A second, and much better approach would be to make use of Python's dictionaries as Shrutarshi Basu wrote about on his website. He makes use of Python's dictionaries to match a key and value, similar to the functionality of a switch statement normally. Take a look at this example he provides to get a better idea:

options = {0 : zero,
                1 : sqr,
                4 : sqr,
                9 : sqr,
                2 : even,
                3 : prime,
                5 : prime,
                7 : prime,
}

def zero():
    print "You typed zero.\n"

def sqr():
    print "n is a perfect square\n"

def even():
    print "n is an even number\n"

def prime():
    print "n is a prime number\n"

It starts by defining all the possible "keys" (values in the dictionary) that the make-shift switch statement would use to trigger the function, and then it defines the functions below based on what "key" (dictionary value) is called.

Once you do that, it's as simple as doing a dictionary lookup:

options[num]()

I highly recommend you read the article that I had linked to as it will help clarify things surrounding Python's switch-case statements, or lack thereof.

Ryan Fitzgerald
  • 423
  • 2
  • 9
  • Can you tell me how to use this code an editor ?i'm using pycharm software that show error :Traceback (most recent call last): File "C:/Users/NeErAj/PycharmProjects/demo/switch.py", line 1, in options = {0 : zero, NameError: name 'zero' is not defined – Neeraj Kumar Sep 20 '15 at 05:36
  • I'm not entirely sure what you mean? Say you were testing a value of `myVar` in the switch-case, what you would do to call the "switch" is to do a dictionary lookup: `options[myVar]()` and it would check the value of `myVar` against the values like a normal switch would and run whichever block it matches. Is that what you mean? – Ryan Fitzgerald Sep 20 '15 at 05:40
  • i want to say that how to use option = {0 ,zero.............. and How to call all methods using option when myVar is assigned any value – Neeraj Kumar Sep 20 '15 at 06:03
  • It's used the same way as you would use a switch statement, it just has slightly different structure. Instead of having the case and the function all in one like you do with a switch, you place the case values above (see options), and then underneath you define what that case value does. It's best to think of all that code as the switch statement functionality and the call as the switch. By that I mean, wherever you would put the switch statement in your code normally, you would put the call to it now (options[myVar]) – Ryan Fitzgerald Sep 20 '15 at 06:13
  • can you compile this code at http://goo.gl/iMK6Sr ? it also give same error. – Neeraj Kumar Sep 20 '15 at 08:33
-1

There is no Switch Case in Python (one of the things that is unfortunate about the language). Instead you are going to have to use elif (else if) statements like below.

if n == 0:
    print "You typed zero.\n"

elif n== 1 or n == 9 or n == 4:
    print "n is a perfect square\n"

elif n == 2:
    print "n is an even number\n"

elif  n== 3 or n == 5 or n == 7:
    print "n is a prime number\n"

else:
    print "You picked a weird number"

Hope that helps!

Gilles
  • 9,269
  • 4
  • 34
  • 53
user7542
  • 111
  • 1
  • 2