-4

I need help with writing a function to_secs that converts hours, minutes, and seconds to a total number of seconds. In the book it says the following test should be possible:

to_secs(2, 30, 10) == 9010
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • What exactly is your problem? Where is your code? – jonrsharpe Feb 24 '14 at 18:15
  • I dont have code for it, Ive only been working on coding for 2 weeks. So Im just very confused – Marco Lourenco Feb 24 '14 at 18:19
  • I am also confused. Please edit your question to make your intention clear. If you are asking whether you should check that the arguments to the function are in appropriate ranges, that will depend on the specification you have been given. – jonrsharpe Feb 24 '14 at 18:21
  • ok sorry, what I would like is python code with the function to_secs that converts hours, minutes and seconds to a final answer of total seconds. – Marco Lourenco Feb 24 '14 at 18:25
  • 1
    Normally, at this point, I would tell you that SO isn't a site to have code written for you, particularly given how trivial a task you have been given. However, Martijn has already done it, so lucky you. – jonrsharpe Feb 24 '14 at 18:27
  • 2
    @jonrsharpe: I'm in a good mood today. I'm not sure if I'll continue to be in that mood, if there are more homework questions of this calibre but for now I am still whistling a merry tune. – Martijn Pieters Feb 24 '14 at 18:31
  • @MartijnPieters glad to hear you're in such fine fettle! – jonrsharpe Feb 24 '14 at 19:13

1 Answers1

2

There are 3600 seconds in an hour, 60 in a minute. The rest is simple arithmetic:

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

Demo:

>>> def to_secs(hours, minutes, seconds):
...     return hours * 3600 + minutes * 60 + seconds
... 
>>> to_secs(2, 30, 10) == 9010
True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • you my good man, are an absolute legend! these may seem meaningless to people that are advanced but im a beginner and I thank you!! – Marco Lourenco Feb 24 '14 at 18:30
  • 3
    And, no, I don't think that downvoting the *answers* on a question you feel isn't suitable to Stack Overflow is the right way to go about things. Vote on the individual post, not the context, please. – Martijn Pieters Feb 24 '14 at 18:45