“Kasing sakelar Python” Kode Jawaban

kasus dalam 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

sakelar 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

Kasing sakelar Python

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

Kasing sakelar 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

Kasing sakelar 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

Kasing sakelar Python

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

Jawaban yang mirip dengan “Kasing sakelar Python”

Pertanyaan yang mirip dengan “Kasing sakelar Python”

Lebih banyak jawaban terkait untuk “Kasing sakelar Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya