如何在Python中使用百分号将秒转换为分钟和秒

3
def main():
    import math
    print('Period of a pendulum')
    Earth_gravity = 9.8
    Mars_gravity = 3.7263
    Jupiter_gravity = 23.12
    print('     ')
    pen = float(input('How long is the pendulum (m)? '))
    if pen < 0:
        print('illegal length, length set to 1')
        pen = 1
        period1 = (2 * 3.14159265359) * math.sqrt(pen / Earth_gravity)
        period2 = (2 * 3.14159265359) * math.sqrt(pen / Mars_gravity)
        period3 = (2 * 3.14159265359) * math.sqrt(pen / Jupiter_gravity)        
        print('     ')
        print('The period is', round(period1,3))
        minutes1 = period1 / 60
        minutes2 = period1 / 60
        minutes3 = period1 / 60
        seconds1 = minutes1 % 60
        seconds2 = minutes2 % 60
        print('or', round(minutes1,1), 'minutes and', seconds, 'seconds on 
Earth')
        print('     ')
        print('The period is', round(period2,3))
        print('or', round(minutes2,1), 'minutes and', seconds, 'seconds on 
Mars')
        print('     ')
        print('The period is', round(period3,3))
        print('or', round(minutes3,1), 'minutes and', seconds, 'seconds on 
Jupiter')        
    else:
        period1 = (2 * 3.14159265359) * math.sqrt(pen / Earth_gravity)
        period2 = (2 * 3.14159265359) * math.sqrt(pen / Mars_gravity)
        period3 = (2 * 3.14159265359) * math.sqrt(pen / Jupiter_gravity)        
        print('     ')
        print('The period is', round(period1,3))
        minutes1 = period1 // 60
        minutes2 = period2 // 60
        minutes3 = period3 // 60
        seconds1 = minutes1 % 60
        seconds2 = minutes2 % 60
        seconds3 = minutes3 % 60
        print('or', round(minutes1,1), 'minutes and', seconds1, 'seconds on 
Earth')
        print('     ')
        print('The period is', round(period2,3))
        print('or', round(minutes2,1), 'minutes and', seconds2, 'seconds on 
Mars')
        print('     ')
        print('The period is', round(period3,3))
        print('or', round(minutes3,1), 'minutes and', seconds3, 'seconds on Jupiter')        

main()

好的,所以我需要将秒转换为秒和分钟。 我不确定如何使用%来获取输出中的秒数和分钟数。 我需要在此使用//和%。 我非常新手,所以如果这很笨拙或过度,请原谅。 包含%的行出了问题。 谢谢!

好的,所以我需要将秒转换为秒和分钟。我不确定如何使用%进行计算,从而获得输出中的秒数和分钟数。在这里,我需要使用//和%进行计算。我是一个新手,如果我的方法显得有些繁琐或者不够简洁,请见谅。出现问题的部分在于包含%的那些行。谢谢!


math.pi 总是比 3.14159265359 好。 - DYZ
已经有不错的答案了,但我注意到你在分钟上使用了%,这样不会给你秒数。你必须从一开始就使用秒数,所以你应该对周期取模,而不是分钟。我建议使用divmod。 - PoDuck
2个回答

8
您可以简单地使用divmod函数,该函数返回整数商和余数,非常适合您的情况:
>>> seconds = 1000
>>> minutes, seconds = divmod(seconds, 60)
>>> hours, minutes = divmod(minutes, 60)
>>> days, hours = divmod(hours, 24)
>>> days, hours, minutes, seconds
(0, 0, 16, 40)

看起来你只需要第一行代码minutes, seconds = divmod(seconds, 60),但是我想展示一下如果有更多的转换也可以如何使用。 :)


3
整数会被取整到最接近的整数。%运算符只报告除法操作的余数。
所以,135%60返回15。这是因为60可以整除135两次,但剩余的15小于60。60除以135的两次结果不会被返回,因此您需要使用标准除法运算符来找到该值。
您可以将其除以60得到分钟,然后还可以使用模运算符返回剩余秒数。
time = 135

minutes = time / 60
seconds = time % 60

print minutes
print seconds

返回值

2
15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接