习题39
python:3.9
class Song(object):
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",
"So I'll stop right there"])
bulls_on_parade = Song(["They rally around the family",
"With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
运行结果
PS C:\Users\78523\mybuff> python ex39.py
Happy birthday to you
I don't want to get sued
So I'll stop right there
They rally around the family
With pockets full of shells
加分习题
1.在 Python 文档中找到 dictionary (又被称作 dicts, dict)的相关的内容,学着对 dict 做更多的操作。
2.找出一些 dict 无法做到的事情。例如比较重要的一个就是 dict 的内容是无序的,你可以检查一下看看是否真是这样。
3.试着把for-loop执行到 dict 上面,然后试着在 for-loop 中使用 dict 的 items()函数,看看会有什么样的结果。
以上均没研究明白,希望大家指点一下
习题39