将秒转换为天,小时,分钟和秒

时间:2019-09-11 01:52:23

标签: python python-3.x

我是Python的新手,遇到了这个问题。我想创建一个程序,用户在其中输入几秒钟,然后将其转换为天,小时,分钟和秒。但是,例如,我希望程序在100,000秒内输出1天3小时46分钟40秒。但是,我也使用小数输入计算结果,例如它将显示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 个答案:

答案 0 :(得分:0)

将每个变量转换为整数应根据需要擦除小数点,因此对所有变量(d,h,m和s)像d = int(x/86400)一样使用它。

答案 1 :(得分:0)

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.
相关问题