“Instruction Python Switch” Réponses codées

cas en python

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"
Concerned Capybara

interrupteur python

# Python 3.10.0 +
match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>
TheMikeste1

cas de commutateur Python

case 401 | 403 | 404:
    return "Not allowed"
Clumsy Cod

interrupteur python

>>> def week(i):
        switcher={
                0:'Sunday',
                1:'Monday',
                2:'Tuesday',
                3:'Wednesday',
                4:'Thursday',
                5:'Friday',
                6:'Saturday'
             }
         return switcher.get(i,"Invalid day of week")<div class="open_grepper_editor" title="Edit & Save To Grepper"></div>
Glorious Gerenuk

Instruction Python Switch

def f(x):
    match x:
        case 'a':
            return 1
        case 'b':
            return 2
        case _:
            return 0   # 0 is the default case if x is not found
Monazil Chowdhury Ayan

Élément de commutateur Python

def operations(letter):
    switch={
       'a': "It is a vowel",
       'e': "It is a vowel",
       'i': "It is a vowel",
       'o': "It is a vowel",
       'u': "It is a vowel",
       }
    return switch.get(letter,"It is a not a vowel")		# disregard 2nd parameter
operations('a')
Tofufu

Réponses similaires à “Instruction Python Switch”

Questions similaires à “Instruction Python Switch”

Plus de réponses similaires à “Instruction Python Switch” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code