You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义Songs类的sing_me方法如何实现将元组元素逐行拆分输出

Python歌词逐行输出实现方案

实现说明

你需要的功能不需要将整个序列转换为字符串,直接遍历存储的歌词元素逐行打印即可,同时修正现有代码的方法定义语法错误即可满足所有要求。

完整可运行代码

class Songs:
    def __init__(self, lyrics):
        self.lyrics = lyrics
    def sing_me(self):
        # 直接遍历歌词序列的每个元素,逐行输出
        for line in self.lyrics:
            # 如需去除元素末尾多余空格可加rstrip(),不需要可直接写print(line)
            print(line.rstrip())

def main():
    # 要求保留的代码行
    Let_go = Songs(['When it"s black, ', 'Take a little time to hold yourself, ', 'Take a little time to feel around, ', 'Before it"s gone!'])
    # 调用方法触发歌词输出
    Let_go.sing_me()

if __name__ == '__main__':
    main()

输出效果

运行代码后将得到指定的逐行输出结果:

When it"s black,
Take a little time to hold yourself,
Take a little time to feel around,
Before it"s gone!

内容的提问来源于stack exchange,提问作者Oliwier Piasecki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 08:24:07