# Before the loop:
totalSeconds = 0
# Print out the track information, one track per line:
for ...:
# Inside of the loop:
# Print out the track information:
print(...)
# Collect the run-length time by adding it to totalSeconds:
mins, secs = map(int, record[3].split(":"))
totalSeconds += mins * 60 + secs
# After the loop:
hours = totalSeconds // 3600
minutes = (totalSeconds // 60) % 60
seconds = totalSeconds % 60
print("TOTAL PLAYING TIME: {}:{:02d}:{:02d}".format(
hours, minutes, seconds))
如果我理解正确的话,你想解决两个问题。首先是如何创建标题,其次是如何使标题顶部和底部水平线与表格大小对齐。请尝试下面的代码。注意使用ljust将字符串填充到给定列的最大长度:
如果您只想将单词“TRACK”、“ALBUM”、“ARTIST”和“TIME”大写,则可以单独打印标题行,如下所示:
然后,在打印出标题之后,您可以跳过for循环中的第一个条目,只需将for循环的第一行更改为:
更新:
至于您的更新,
totalmins
和totalsecs
都被初始化为零,然后您稍后设置它们,只使用totalmins
和totalsecs
,而从不使用值mins
和secs
。我不认为这是你想要的。你知道吗我认为您试图通过在循环中累积来收集总播放时间,然后在循环完成时以可读的格式输出它。你知道吗
如果是这样的话,最好也显示小时数。因此,考虑创建一个存储所有秒数的变量(我称之为
totalSeconds
),然后计算hours
、minutes
和seconds
:相关问题 更多 >
编程相关推荐