“cas de commutateur 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

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

cas de commutateur Python

Point(1, var)
Point(1, y=var)
Point(x=1, y=var)
Point(y=var, x=1)
Clumsy Cod

Réponses similaires à “cas de commutateur Python”

Questions similaires à “cas de commutateur Python”

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

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code