【发布时间】:2013-11-21 03:49:50
【问题描述】:
使用的元组如下:
Album = namedtuple('Album', 'id artist title year songs')
# id is a unique ID number; artist and title are strings; year is a number,
# the year the song was released; songs is a list of Songs
Song = namedtuple('Song', 'track title length play_count')
# track is the track number; title is a string; length is the number of
# seconds long the song is; play_count is the number of times the user
# has listened to the song
def Song_str(S: Song)->str:
'''Takes a song and returns a string containing that songs
information in an easily readible format'''
return '{:12}{}\n{:12}{}\n{:12}{}\n{:12}{}'.format('Track:', S.track,
'Title:', S.title,
'Length:', S.length,
'Play Count:', S.play_count)
def Album_str(a: Album)->str:
'''Takes an album and returns a string containing that songs
information in an easily readible format'''
Album_Songs = a.songs
for i in range(0, len(Album_Songs)):
String = Song_str(Album_Songs[i])+ '\n'
return '{:10}{}\n{:10}{}\n{:10}{}\n{:10}{}\n\n{}\n{}'.format('ID:', a.id,
'Artist:', a.artist,
'Title:', a.title,
'Year:', a.year,
'List of Songs:', String)
print(Album_str(AN ALBUM))
专辑信息打印正常,但是当它打印专辑的歌曲时,它是元组歌曲的列表,它将打印该列表中的第一首或最后一首歌曲信息
【问题讨论】:
-
这不是正确的 Python 语法。
-
@HennyH:在我看来没有什么明显的错误。也许你不知道function annotations?我没有使用过 Python 3,所以我不太确定如何使用它们,但这些对我来说就像函数注释。