# Python 3.10 引入了 match-case 语法,可以实现类似 switch 的功能
def http_status_code(status):
match status:
case 200:
return "OK"
case 301 | 302:
return "Redirect"
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 后面跟着匹配的模式和对应的代码块。| 来表示多个值的匹配(如 301 | 302)。_ 是默认情况,类似于 default,当没有匹配到任何情况时执行。上一篇:python 全局变量
下一篇:python 随机数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站