Python Forum

Full Version: switch limitations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is it really not possible to switch on multiple conditions?

    switch (id) {
        case 1, 2, 3, 4, 17, 18, 19, 20:
            print "first switch"
        case 5, 21, 37, 53, 69, 85, 101, 117:
            print "second switch"
    }
This doesn't parse. Is there a way to do this, or do I need to write my own switch defs?

Thanks.
no python does not have a switch
see here

in a case like that i would just use the in operator
if id in [1, 2, 3, 4, 17, 18, 19, 20]:
    print("first switch")
elif id in [5, 21, 37, 53, 69, 85, 101, 117]:
    print("second switch")
However if you are mapping one on one, then a dictionary would better suffice as described in the link provided.
And note that id() is built-in function, so don't use it as variable name
Python does not have a switch operator.You can use it with a simple if statement instead.