“Python Switch Case” Code-Antworten

Fall in 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

Python -Schalter

# 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

Python Switch Case

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

Python Switch Case

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

Python Switch Case

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

Python Switch Case

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

Ähnliche Antworten wie “Python Switch Case”

Fragen ähnlich wie “Python Switch Case”

Weitere verwandte Antworten zu “Python Switch Case” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen