Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
switch limitations
#1
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.
Reply
#2
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.
Recommended Tutorials:
Reply
#3
And note that id() is built-in function, so don't use it as variable name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Python does not have a switch operator.You can use it with a simple if statement instead.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytrends related querie & suggestion limitations cmg15326 0 583 May-02-2023, 03:47 PM
Last Post: cmg15326
  best way to use switch case? korenron 8 2,981 Aug-18-2021, 03:16 PM
Last Post: naughtyCat
  Fraction Calculation with Limitations TINMAN01 13 5,349 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  How do I do this? Switch Case? mstichler 4 2,567 Jun-05-2020, 10:27 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020