如何使桌子更漂亮

2024-04-20 01:06:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在底部添加“总播放时间:00:0:0”,我尝试的命令已更新如下:

totalmins = 0
totalsecs = 0
mins, secs = map(int, record[3].split(":")) 
totalmins = totalmins + (totalsecs // 60)
totalsecs = totalsecs % 60

显示错误缩进有问题


Tags: 命令map错误时间recordintsplitsecs
2条回答

如果我理解正确的话,你想解决两个问题。首先是如何创建标题,其次是如何使标题顶部和底部水平线与表格大小对齐。请尝试下面的代码。注意使用ljust将字符串填充到给定列的最大长度:

hl_len = max_track + max_artist + max_album + 4
print ('-' * hl_len)

for idx, i in enumerate(data):

    print("|", i[0].ljust(max_track), 
          "|", i[1].ljust(max_artist), 
          "|", i[2].ljust(max_album), 
          "|", i[3],"|")
    if idx == 0:
        print ("-" * hl_len) 

如果您只想将单词“TRACK”、“ALBUM”、“ARTIST”和“TIME”大写,则可以单独打印标题行,如下所示:

print("| TRACK", " " * (max_track - 5),
      "| ARTIST", " " * (max_artist - 6),
      "| ALBUM", " " * (max_album - 5),
      "| TIME |")

然后,在打印出标题之后,您可以跳过for循环中的第一个条目,只需将for循环的第一行更改为:

for i in data[1:]:

更新:

至于您的更新,totalminstotalsecs都被初始化为零,然后您稍后设置它们,只使用totalminstotalsecs,而从不使用值minssecs。我不认为这是你想要的。你知道吗

我认为您试图通过在循环中累积来收集总播放时间,然后在循环完成时以可读的格式输出它。你知道吗

如果是这样的话,最好也显示小时数。因此,考虑创建一个存储所有秒数的变量(我称之为totalSeconds),然后计算hoursminutesseconds

# 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))

相关问题 更多 >