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

前端 未结 16 1419
不思量自难忘°
不思量自难忘° 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 08:10

    seconds_in_day = 86400
    seconds_in_hour = 3600
    seconds_in_minute = 60
    
    seconds = int(input("Enter a number of seconds: "))
    
    days = seconds // seconds_in_day
    seconds = seconds - (days * seconds_in_day)
    
    hours = seconds // seconds_in_hour
    seconds = seconds - (hours * seconds_in_hour)
    
    minutes = seconds // seconds_in_minute
    seconds = seconds - (minutes * seconds_in_minute)
    
    print("{0:.0f} days, {1:.0f} hours, {2:.0f} minutes, {3:.0f} seconds.".format(
        days, hours, minutes, seconds))
    

提交回复
热议问题