第二关 Python基础
task1
wordcount函数
import re
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
def wordcount(text):
text = text.lower()
text = re.sub('[,.!?\n]',"",text)
text_list = text.split()
text_dict = {}
for item in text_list:
if item not in text_dict.keys():
text_dict[item] = 1
else:
text_dict[item] += 1
print(text_dict)
return text_dict
wordcount(text)
task2
Vscode连接InternStudio debug笔记
case1: debug单个文件
- 设置断点
- 点击左侧运行与调试栏,调试当前文件
- 按照需要选择调试方式
case2:使用命令行debug
- 点击VSCode侧边栏的“Run and Debug”(运行和调试),单击"create a lauch.json file"
- 选择debugger时选择python debuger。选择debug config时选择remote attach
安装一下库:pip install debugpy
python -m debugpy --listen 5678 --wait-for-client ./myscript.py
./myscript.py
替换为想要debug的python文件,后面可以和直接在命令行中启动python一样跟上输入的参数。记得要先在想要debug的python文件打好断点并保存。--wait-for-client
参数会让我们的debug server在等客户端连入后才开始运行debug。在这就是要等到我们在run and debug界面启动debug。
先在终端中发起debug server,然后再去vscode debug页面单击一下绿色箭头开启debug。
使用别名简化操作
在linux
系统中,可以对 ~/.bashrc 文件中添加以下命令
alias pyd='python -m debugpy --wait-for-client --listen 5678'
然后执行
source ~/.bashrc
这样之后使用 pyd 命令(你可以自己命名) 替代 python 就能在命令行中起debug了,之前的debug命令就变成了
pyd ./myscript.py