学习 Python 3 the Hard Way 第 40 课的课程。我正在尽我最大的努力来理解这个“for循环”是如何计算的。
“对于 self.lyrics 中的行:打印(行)”
我还想知道如何将“行”转换为数字,以便可以在歌词行顶部打印行号。
我的轻微修改是添加另一行“你为什么脏老鼠”,看看它是否会按预期打印。我还删除了逗号,并按预期添加了该行
class Song():
def __init__(self, lyrics):
self.lyrics = lyrics
----------------------------------------------------
def sing_me_a_song(self):
for line in self.lyrics:
print(line)
----------------------------------------------------
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued ",
"Why you dirty rat",
"So ill stay right there"])
bulls_on_parade = Song(["They rally around tha family",
"With pockets full of shells"])
print("\n")
happy_bday.sing_me_a_song()
print("\n")
bulls_on_parade.sing_me_a_song()
print("\n")```
长风秋雁
慕桂英546537
相关分类