-1

I want to implement a code in Python but I need to implement a switch case. I saw this online (a standard example):

def switch_demo(argument):
    switcher = {
        1: "January",
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        12: "December"
    }
    print switcher.get(argument, "Invalid month")

But I need something different. I don't want to display some strings but to execute some commands, like:

switch(button) { // this is a code for C structure
case 1: do some things ( not just display text)
case 2: do some things ( not just display text)

And so on. Can you please give me an example of how this structure is supposed to look?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    You can store *functions* as the values in the dictionary, and call them: https://stackoverflow.com/a/10272369/3001761 – jonrsharpe Sep 18 '19 at 07:39

1 Answers1

1

Typically, where other languages use switch, Python uses if...elif...else... statement.

if button == 1:
    stuff()
elif button == 2:
    more_stuff()
elif button == 3:
    other_stuff()
else:
    no_stuff()
Amadan
  • 191,408
  • 23
  • 240
  • 301