“cas en python” 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

cas de commutateur Python

class Point:
    x: int
    y: int

def location(point):
    match point:
        case Point(x=0, y=0):
            print("Origin is the point's location.")
        case Point(x=0, y=y):
            print(f"Y={y} and the point is on the y-axis.")
        case Point(x=x, y=0):
            print(f"X={x} and the point is on the x-axis.")
        case Point():
            print("The point is located somewhere else on the plane.")
        case _:
            print("Not a point")
Clumsy Cod

cas de commutateur Python

match points:
    case []:
        print("No points in the list.")
    case [Point(0, 0)]:
        print("The origin is the only point in the list.")
    case [Point(x, y)]:
        print(f"A single point {x}, {y} is in the list.")
    case [Point(0, y1), Point(0, y2)]:
        print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
    case _:
        print("Something else is found in the list.")
Clumsy Cod

Réponses similaires à “cas en python”

Questions similaires à “cas en python”

Plus de réponses similaires à “cas en python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code