InternLM-L0 Python作业

任务类型 任务内容

闯关任务 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

 

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wL魔法师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值