81
uuid
7y

So Python doesn't have switch-case statement... I feel so dirty having to use multiple if-elif-else

*sigh*

Comments
  • 41
    Use a dictionary and put the code to execute in functions:

    def funcA():
    startWorldDomination()

    def funcB():
    raiseUnicorns()

    def funcC():
    rmRfRoot()

    dictCase ={ 1:funcA, 2:funcB, 3:funcC }

    searchItem = input("Enter number from 1-3")
    dictCase[searchItem]()
  • 3
    @ddephor nice example, I'll make sure to try it out.
  • 1
    @irene yes I agree, but in my current use case (comparing a value with a bunch of constants and for each constant do something) I definitely need the switch- case statement.
    But ifs statements can also be used ofc 😅
  • 4
    I've been thinking about possibly learning python, guess this is another thing for me to dislike about it.
  • 10
    @Cieric If you let this discourage you from learning python, then I feel sorry for you man/woman.

    Python rocks.
  • 0
    @rising Well it has always been something I though about learning but I've never really had a HUGE use for it. I've used it for a bunch of small projects (like 30 lines or so) But it's been so sparse that I never memorize the syntax. On top of all that I work on a lot of high performance applications so python has always been a little slow.

    Oh and don't get me wrong I do plan on learning python enough to read over programs, but using it on a daily basis is something I'd like to avoid for now.
  • 3
    @Cieric you do you buddy.
    It's all about will and motivation ✌
  • 0
    @rising 👌
  • 0
  • 1
    Python is beautiful 😍
  • 1
    @Cyanide indeed, I feel so happy and motivated ever since I took over this python project at work 😆
  • 1
    @ddephor I have never seen anything like that!!! It's cool! :D
Add Comment