提交 fff4b711 编写于 作者: F feilong

fix bug

上级 13537fb9
{
"source": "match_01.py",
"author": "huanhuilong",
"source": "match_01.md",
"depends": [],
"exercise_id": 221,
"type": "code_options"
......
# Python 结构化模式匹配(1)
Python 3.10 引入了结构化模式匹配。请使用 Python 3.10+ 版本测试本代码。
## template
```python
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['push', 'pull', 'commit'])
args = parser.parse_args()
match args.command:
case 'push':
print('pushing')
case 'pull':
print('pulling')
case _:
parser.error(f'{args.command!r} not yet implemented')
```
## 答案
```python
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['push', 'pull', 'commit'])
args = parser.parse_args()
match args.command:
case 'push':
print('pushing')
case 'pull':
print('pulling')
case _:
parser.error(f'{args.command!r} not yet implemented')
```
## 选项
### A
```python
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['push', 'pull', 'commit'])
args = parser.parse_args()
switch args.command:
case 'push':
print('pushing')
case 'pull':
print('pulling')
case _:
parser.error(f'{args.command!r} not yet implemented')
```
### B
```python
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['push', 'pull', 'commit'])
args = parser.parse_args()
match args.command:
case 'push':
print('pushing')
case 'pull':
print('pulling')
default:
parser.error(f'{args.command!r} not yet implemented')
```
### C
```python
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['push', 'pull', 'commit'])
args = parser.parse_args()
match args.command:
if 'push':
print('pushing')
elif 'pull':
print('pulling')
else _:
parser.error(f'{args.command!r} not yet implemented')
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册