如何在 python 中将 hh:mm:ss 转换为没有 time_string 的秒数?

How to convert a hh:mm:ss to seconds without a time_string in python?

对于我的作业,要求是这样陈述函数:
def to_seconds(hours, minutes, seconds):

我看过的大多数节目运行它在括号之间带有time_str。我一直在尝试一些不同的可能性,但我似乎无法理解为什么我的列表不想转换为整数,所以我可以计算所有单个变量。

这是我的代码:

def to_seconds(hours, minutes, seconds):
  hh, mm, ss = str((hours * 3600, minutes * 60, seconds,)).split()
  return int(hh) + int(mm) + int(ss)

对不起,我在评论的帮助下弄明白了。这似乎对我有用:

def to_seconds(hours, minutes, seconds):
    hh, mm, ss = (hours * 3600, minutes * 60, seconds,)
    return int(hh) + int(mm) + int(ss)

您增加了不必要的复杂性: 只是做:

def to_seconds(hours, minutes, seconds):
  return hours*3600 + minutes*60 + seconds

您当前的错误是:

hh = '(0,'
mm = '60,'
ss = '0)'

如您所知,我们无法将“(0,”,“60,”或“0)”转换为整数。