Python function to convert seconds into minutes, hours, and days

前端 未结 16 1409
不思量自难忘°
不思量自难忘° 2020-11-28 07:27

Question: Write a program that asks the user to enter a number of seconds, and works as follows:

  • There are 60 seconds in a minute. If the number of seconds

16条回答
  •  情话喂你
    2020-11-28 07:57

    Although divmod() has been mentioned, I didn't see what I considered to be a nice example. Here's mine:

    q=972021.0000  # For example
    days = divmod(q, 86400) 
    # days[0] = whole days and
    # days[1] = seconds remaining after those days
    hours = divmod(days[1], 3600)
    minutes = divmod(hours[1], 60)
    print "%i days, %i hours, %i minutes, %i seconds" % (days[0], hours[0], minutes[0], minutes[1])
    

    Which outputs:

    11 days, 6 hours, 0 minutes, 21 seconds
    

提交回复
热议问题