Python 的 match 语句
等级 3126 次按键
定义一个使用 match 将值分类为 zero、int 或 other 的函数。
Python 代码
1def kind(x): # match 按从上到下的顺序尝试每个 case 模式2 match x:3 case 0:4 return "zero" # int() 匹配其余所有整数,所以 7 会落在这里5 case int():6 return "int" # _ 是匹配其他所有值的通配符,例如字符串 a7 case _:8 return "other"9print(kind(0), kind(7), kind("a"))运行结果
zero int other
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 Python 代码中需要输入的符号及其出现次数。
"× 8(× 6)× 6:× 5,× 2_× 1