# Python match-case 示例代码
# 定义一个函数,使用 match-case 语句来处理不同的输入
def http_status_code(status_code):
match status_code:
case 200:
return "OK"
case 400:
return "Bad Request"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
case _:
return "Unknown Status Code"
# 测试函数
print(http_status_code(200)) # 输出: OK
print(http_status_code(404)) # 输出: Not Found
print(http_status_code(999)) # 输出: Unknown Status Code
match-case 是 Python 3.10 引入的新语法,类似于其他语言中的 switch-case 语句。match 关键字后面跟的是要匹配的表达式,case 后面是模式。当表达式的值与某个模式匹配时,执行相应的代码块。_ 是默认模式,表示如果没有任何模式匹配,则执行该代码块。http_status_code,它根据传入的 HTTP 状态码返回相应的描述信息。上一篇:python的变量名
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站