第二关 Python基础

第二关 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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值