任务类型 任务内容
闯关任务 Python实现wordcount
闯关任务 Vscode连接InternStudio debug笔记
1. Python实现wordcount
基于作业 InternLM-L0 linux作业 ,创建Python文件输入如下代码,并用Python 脚本运行:

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 word_count(text):
    # 将文本转换为小写,并将标点符号替换为空格
    text = text.lower().replace(",", " ").replace(".", " ").replace("!", " ").replace("?", " ")
 
    # 利用空格将文本分割成单词列表
    words = text.split()
 
    # 创建一个空字典,用于存储每个单词的计数
    word_count = {}
 
    # 遍历单词列表,统计每个单词的出现次数
    for word in words:
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1
 
    return word_count
 
# 示例用法
text = "This is a sample sentence. This sentence is used for word count example."
result = word_count(text)
print(result)2. Debug断点
1. 设置断点
在代码行号旁边点击,可以添加一个红点,这就是断点(如果不能添加红点需要检查一下python extension是否已经正确安装)。当代码运行到这里时,它会停下来,这样你就可以检查变量的值、执行步骤等。

2.启动debug
点击VSCode侧边栏的“Run and Debug”(运行和调试),然后点击“Run and Debug”(开始调试)按钮,或者按F5键。

单击后会需要选择debugger和debug配置文件,我们单独debug一个python文件只要选择Python File就行。然后你的代码会在达到第一个断点之前运行,在第一个断点处停下来。当代码在断点处停下来时,你可以查看和修改变量的值。在“Run and Debug”侧边栏的“Variables”(变量)部分,你可以看到当前作用域内的所有变量及其值。

3.单步调试
你可以使用“Run and Debug”侧边栏顶部的按钮来单步执行代码。这样,你可以逐行运行代码,并查看每行代码执行后的效果。

debug面板各按钮功能介绍:
1: continue: 继续运行到下一个断点
2: step over:跳过,可以理解为运行当前行代码,不进入具体的函数或者方法。
3: step into: 进入函数或者方法。如果当行代码存在函数或者方法时,进入代码该函数或者方法。如果当行代码没有函数或者方法,则等价于step over。
4: step out:退出函数或者方法, 返回上一层。
5: restart:重新启动debug
6: stop:终止debug



















