分享
三行代码  ›  专栏  ›  技术社区  ›  AduH

将秒转换为天、小时、分钟和秒 - Converting Seconds into days, hours, minutes, and seconds

  •  0
  • AduH  · 技术社区  · 1 周前

    我是python新手,遇到了这个问题。我想创建一个用户输入秒数的程序,并将其转换为天、小时、分钟和秒。但是,例如,我希望100000秒的程序输出1天3小时46分40秒。但是,mine也用小数来输入计算结果,比如1.157天,其他类别也一样。任何帮助都将不胜感激,非常感谢。

    def SecondsConvertor(x):
        d = x/86400
        h = (x%86400)/3600
        m = (x%3600)/60
        s = (x%60)
        print("Your input is equal to", d, "days,", h, "hours,", m, "minutes,", "and", s, "seconds.")
    x = 100000
    
    SecondsConvertor(x)
    
    2 回复  |  直到 1 周前
        1
  •  0
  •   Matheus Melo    1 周前

    将每个变量转换为整数时,应根据需要删除小数,因此请按如下方式使用 d = int(x/86400) 所有变量(d,h,m和s)。

        2
  •  0
  •   MegaEmailman    1 周前
    def SecondConverter(x):
        d = int(x/86400)  #The int call removes the decimals.  Conveniently, it always rounds down.  int(2.9) returns 2 instead of 3, for example.
        x-=(d*86400)  #This updates the value of x to show that the already counted seconds won't be double counted or anything.
        h = int(x/3600)
        x-=(h*3600)
        m = int(x/60)
        x -= (m*60)
        s = x
        print("Your input is equal to ", d, " days, ", h, " hours, ", m, " minutes, and ", s, "seconds.")
    
    SecondConverter(100000)
    
    #gives 1 day, 3 hours, 46 minutes, and 40 seconds.