书生大模型实战营第三期闯关作业打卡(Python 基础知识)

文档:Tutorial/docs/L0/Python at camp3 · InternLM/Tutorial · GitHub

视频:Python 前置基础知识

Python 前置基础知识_哔哩哔哩_bilibili

作业:Tutorial/docs/L0/Python/task.md at camp3 · InternLM/Tutorial · GitHub
                        

任务一:

使用本地VScode和插件Remote-SSH 远程连接到InternLM开发机。打开文件到root目录,在root目录下创建wordcount.py。

以下是wordcount.py的代码:

import re

def wordcount(text):

    text = re.sub(r'[^\w\s]','',text)

    text = text.lower()

    text = text.split()

    counts = {}

    for i in text:

        if i not in counts:

            counts[i] = 1

        else:

            counts[i] += 1

    return counts

if __name__ == '__main__':

    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.

    """

    print(wordcount(text))

用Run Python --> Run Python File in Terminal 运行一下wordcount.py 看看有没有报错。结果出来了,代码没有问题。

任务二 (1):

在Vscode debug 单个python file,试试debug刚才写的wordcount.py,设置断点之后在右上角选Python Debugger: Debug Python File:

点击Run and Debug之后开始debug:可以在terminal那里看到程序运行到断点上面一行输出的结果。

debug能发现代码里的错误,把句子分解成单词之后,text 不再是一个句子,而是由每个单词组成的list,所以直接这样print会出错。

修正后的wordcount.py:

import re

def wordcount(text):

    print('原来输入的句子: ' + text)

    text = re.sub(r'[^\w\s]','',text)

    print('去掉标点符号之后: ' + text)

    text = text.lower()

    print('所有字母都变成小写字母之后: ' + text)

    words = text.split()

    print('把句子分解成单词: ' + str(words))

    counts = {}

    for i in words:

        if i not in counts:

            counts[i] = 1

        else:

            counts[i] += 1

    return counts

 

if __name__ == '__main__':

    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.

    """

    print(wordcount(text))

修正错误后重新debug

修正后debug就没有出现错误提示了。

任务二 (2):

使用命令行开启debug server, create a launch json file ->Python Debugger ->Remote Attach ->localhost ->5768

配置完成后,pip install debugpy

(base) root@intern-studio-50138422:~# python -m debugpy --listen 5678 --wait-for-client wordcount.py

并且连接到debug derver

开启debug server 跟之前debug单个python file的功能是一样的。

最终的结果:

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值